Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
phaase-hhi committed Jan 15, 2024
2 parents f2f5305 + fdec546 commit 89fd6dd
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ source env/bin/activate

**Note**: For further information on how to set up a virtual python environment (also on **Windows**) refer to https://docs.python.org/3/library/venv.html .

When successfully installed, the software outputs the line : "Successfully installed NNC-0.2.2"
When successfully installed, the software outputs the line : "Successfully installed NNC-0.3.0"

### Importing the main module

Expand Down
8 changes: 8 additions & 0 deletions create_venv_macos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

python3 -m venv env
source env/bin/activate
pip install --upgrade pip
pip install -r requirements_macos.txt
pip install -e .
deactivate
14 changes: 9 additions & 5 deletions framework/pytorch_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,14 @@ def __init__(self,
lr=1e-4,
):

self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available():
device = "cuda"
elif torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"

self.device = torch.device(device)

torch.manual_seed(451)
torch.backends.cudnn.deterministic = True
Expand All @@ -491,7 +498,6 @@ def __init__(self,
self.learning_rate = lr
self.epochs = epochs
self.max_batches = max_batches

self.handle = handler
if test_set:
self.test_set = test_set
Expand All @@ -517,7 +523,6 @@ def test_model(self,
verbose=False
):

torch.set_num_threads(1)
Model = copy.deepcopy(self.model)

base_model_arch = Model.state_dict()
Expand Down Expand Up @@ -556,7 +561,6 @@ def eval_model(self,
verbose=False
):

torch.set_num_threads(1)

Model = copy.deepcopy(self.model)

Expand Down Expand Up @@ -595,7 +599,7 @@ def tune_model(
ft_flag=False,
verbose=False,
):
torch.set_num_threads(1)

verbose = 1 if (verbose & 1) else 0

base_model_arch = self.model.state_dict()
Expand Down
33 changes: 17 additions & 16 deletions framework/tensorflow_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def create_NNC_model_instance_from_file(
model_struct = loaded_model_struct

if dataset_path and model_struct:
if model_name == None and hasattr(model_struct, 'name'):
model_name=model_struct.name
TEFModelExecuter = create_imagenet_model_executer(model_struct=model_struct,
dataset_path=dataset_path,
lr=lr,
Expand Down Expand Up @@ -161,6 +163,8 @@ def create_NNC_model_instance_from_object(
model_struct = loaded_model_struct

if dataset_path and model_struct:
if model_name == None and hasattr(model_struct, 'name'):
model_name=model_struct.name
TEFModelExecuter = create_imagenet_model_executer(model_struct=model_struct,
dataset_path=dataset_path,
lr=lr,
Expand Down Expand Up @@ -230,6 +234,10 @@ def __init__(self, model_dict=None):
def load_model( self,
model_path
):

try:
model_file = tf.keras.models.load_model(model_path)
except:
model_file = h5py.File(model_path, 'r')

try:
Expand Down Expand Up @@ -262,26 +270,19 @@ def init_model_from_model_object( self,
model_object,
):
self.model = model_object

h5_model_path = './temp.h5'
model_object.save_weights(h5_model_path)
model = h5py.File(h5_model_path, 'r')
os.remove(h5_model_path)

if 'layer_names' in model.attrs:
module_names = [n for n in model.attrs['layer_names']]

weights = model_object.get_weights()
layer_names = []
for mod_name in module_names:
layer = model[mod_name]
if 'weight_names' in layer.attrs:
weight_names = [mod_name+'/'+n for n in layer.attrs['weight_names']]
if weight_names:
layer_names += weight_names

for layer in model_object.layers:
mod_name = layer.name
if layer.weights != []:
for weight in layer.weights:
layer_names.append(mod_name+"/"+weight.name)

model_parameter_dict = {}
for name in layer_names:
model_parameter_dict[name] = model[name]
for i, name in enumerate(layer_names):
model_parameter_dict[name] = weights[i]

return self.init_model_from_dict( model_parameter_dict ), model_object

Expand Down
38 changes: 20 additions & 18 deletions framework/use_case_init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,47 +148,49 @@ def preprocess(
):
image_size = 224

if self.__model_name == 'EfficientNetB1':
if self.__model_name == 'EfficientNetB1' or self.__model_name == 'efficientnetb1':
image_size = 240
elif self.__model_name == 'EfficientNetB2':
elif self.__model_name == 'EfficientNetB2' or self.__model_name == 'efficientnetb2':
image_size = 260
elif self.__model_name == 'EfficientNetB3':
elif self.__model_name == 'EfficientNetB3' or self.__model_name == 'efficientnetb3':
image_size = 300
elif self.__model_name == 'EfficientNetB4':
elif self.__model_name == 'EfficientNetB4' or self.__model_name == 'efficientnetb4':
image_size = 380
elif self.__model_name == 'EfficientNetB5':
elif self.__model_name == 'EfficientNetB5' or self.__model_name == 'efficientnetb5':
image_size = 456
elif self.__model_name == 'EfficientNetB6':
elif self.__model_name == 'EfficientNetB6' or self.__model_name == 'efficientnetb6':
image_size = 528
elif self.__model_name == 'EfficientNetB7':
elif self.__model_name == 'EfficientNetB7' or self.__model_name == 'efficientnetb7':
image_size = 600

image, label = self.model_transform(image, label, image_size=image_size)

if 'DenseNet' in self.__model_name:
if 'DenseNet' in self.__model_name or 'densenet' in self.__model_name:
return tf.keras.applications.densenet.preprocess_input(image), label
elif 'EfficientNet' in self.__model_name:
elif 'EfficientNet' in self.__model_name or 'efficientnet' in self.__model_name:
return tf.keras.applications.efficientnet.preprocess_input(image), label
elif self.__model_name == 'InceptionResNetV2':
elif self.__model_name == 'InceptionResNetV2' or self.__model_name == 'inception_resnet_v2':
return tf.keras.applications.inception_resnet_v2.preprocess_input(image), label
elif self.__model_name == 'InceptionV3':
elif self.__model_name == 'InceptionV3' or self.__model_name == "inception_v3":
return tf.keras.applications.inception_v3.preprocess_input(image), label
elif self.__model_name == 'MobileNet':
elif self.__model_name == 'MobileNet' or ( 'mobilenet' in self.__model_name and 'v2' not in self.__model_name ):
return tf.keras.applications.mobilenet.preprocess_input(image), label
elif self.__model_name == 'MobileNetV2':
elif self.__model_name == 'MobileNetV2' or 'mobilenetv2' in self.__model_name:
return tf.keras.applications.mobilenet_v2.preprocess_input(image), label
elif 'NASNet' in self.__model_name:
return tf.keras.applications.nasnet.preprocess_input(image), label
elif 'ResNet' in self.__model_name and 'V2' not in self.__model_name:
elif ('ResNet' in self.__model_name and 'V2' not in self.__model_name) or ('resnet' in self.__model_name and 'v2' not in self.__model_name):
return tf.keras.applications.resnet.preprocess_input(image), label
elif 'ResNet' in self.__model_name and 'V2' in self.__model_name:
elif ('ResNet' in self.__model_name and 'V2' in self.__model_name) or ('resnet' in self.__model_name and 'v2' in self.__model_name):
return tf.keras.applications.resnet_v2.preprocess_input(image), label
elif self.__model_name == 'VGG16':
elif self.__model_name == 'VGG16' or self.__model_name == 'vgg16':
return tf.keras.applications.vgg16.preprocess_input(image), label
elif self.__model_name == 'VGG19':
elif self.__model_name == 'VGG19' or self.__model_name == 'vgg19':
return tf.keras.applications.vgg19.preprocess_input(image), label
elif self.__model_name == 'Xception':
elif self.__model_name == 'Xception' or self.__model_name == 'xception':
return tf.keras.applications.xception.preprocess_input(image), label
elif 'RegNet' in self.__model_name or 'regnet' in self.__model_name:
return tf.keras.applications.regnet.preprocess_input(image), label


# supported use cases
Expand Down
2 changes: 1 addition & 1 deletion requirements_cu11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ scikit-learn>=0.23.1
tqdm>=4.32.2
h5py>=3.1.0
pybind11>=2.6.2
tensorflow>=2.6.0
tensorflow[and-cuda]>=2.6.0
pandas>=1.0.5
opencv-python>=4.4.0.46
torch>=1.8.1
Expand Down
12 changes: 12 additions & 0 deletions requirements_macos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
urllib3<2.0
Click>=7.0
scikit-learn>=0.23.1
tqdm>=4.32.2
h5py>=3.1.0
pybind11>=2.6.2
tensorflow>=2.13.0
tensorflow-metal>=1.0.0
pandas>=1.0.5
opencv-python>=4.4.0.46
torch>=1.12.0
torchvision>=0.13.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from setuptools.command.build_ext import build_ext
import setuptools

__version__ = '0.2.2'
__version__ = '0.3.0'


class get_pybind_include(object):
Expand Down

0 comments on commit 89fd6dd

Please sign in to comment.