Best Python code snippet using dbt-osmosis_python
model.py
Source:model.py
2from abc import ABC, abstractmethod3class ImageBaseModel(ABC):4 @classmethod5 @abstractmethod6 def get_base_model(cls, h:int,w:int,c:int):7 """ãã¼ã¹ã¨ãªãã¢ãã«ãæä¾ããã8 9 Args:10 ImageBaseModel ([type]): [description]11 h (int): å
¥åãµã¤ãº hight12 w (int): å
¥åãµã¤ãº width13 c (int): å
¥åãµã¤ãº channel14 15 Returns:16 [type]: CNNã¢ãã«17 """18 raise NotImplementedError()19class ClasificationModel(ABC):20 @classmethod21 @abstractmethod22 def get_classification_model(cls, base_model:tf.keras.Model)->tf.keras.Model:23 """ ãã¼ã¹ã¢ãã«ã«åé¡é¨åãã¤ããåé¡ç¨ã®ã¢ãã«ãæä¾ããã24 25 Args:26 base_model (tf.keras.Model): ãã¼ã¹ã¢ãã«27 classes (int): åé¡æ°28 29 Returns:30 tf.keras.Model: åé¡ã¢ãã«31 """32 raise NotImplementedError()33class SimpleCNN(ImageBaseModel):34 @classmethod35 def get_base_model(cls, h:int,w:int,c:int):36 """åç´ãªCNNã¢ãã«ãæä¾ããã37 38 Args:39 ImageBaseModel ([type]): [description]40 h (int): å
¥åãµã¤ãº hight41 w (int): å
¥åãµã¤ãº width42 c (int): å
¥åãµã¤ãº channel43 44 Returns:45 [type]: CNNã¢ãã«46 """47 input_layer = tf.keras.Input(shape=(h,w,c),name='input',dtype=tf.float32)48 x = tf.keras.layers.Conv2D(32, (3, 3), padding='same', name='conv2d-1')(input_layer)49 x = tf.keras.layers.Activation('relu')(x)50 x = tf.keras.layers.Conv2D(32, (3, 3), padding='same', name='conv2d-2')(x)51 x = tf.keras.layers.Activation('relu')(x)52 x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2),name='maxpool-1')(x)53 x = tf.keras.layers.Dropout(0.25)(x)54 x = tf.keras.layers.Conv2D(32, (3, 3), padding='same', name='conv2d-3')(x)55 x = tf.keras.layers.Activation('relu')(x)56 x = tf.keras.layers.Conv2D(32, (3, 3), padding='same', name='conv2d-4')(x)57 x = tf.keras.layers.Activation('relu')(x)58 x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2),name='maxpool-2')(x)59 return tf.keras.Model(input_layer,x)60class ResNet50(ImageBaseModel):61 @classmethod62 def get_base_model(cls, h:int,w:int,c:int, weights:bool=False):63 """ResNet CNNã¢ãã«ãæä¾ããã64 65 Args:66 ImageBaseModel ([type]): [description]67 h (int): å
¥åãµã¤ãº hight68 w (int): å
¥åãµã¤ãº width69 c (int): å
¥åãµã¤ãº channel70 71 Returns:72 [type]: CNNã¢ãã«73 """74 if weights==False:75 w_imagenet = None76 else:77 w_imagenet = "imagenet"78 model = tf.keras.applications.ResNet50(include_top=False,input_shape=(h,w,c),weights=w_imagenet)79 return model80class SimpleSoftmaxClassificationModel(ClasificationModel):81 @classmethod82 def get_classification_model(cls, base_model:tf.keras.Model,classes:int)->tf.keras.Model:83 """ ãã¼ã¹ã¢ãã«ã«åé¡é¨åãã¤ããåé¡ç¨ã®ã¢ãã«ãæä¾ããã84 85 Args:86 base_model (tf.keras.Model): ãã¼ã¹ã¢ãã«87 classes (int): åé¡æ°88 89 Returns:90 tf.keras.Model: åé¡ã¢ãã«91 """92 x = tf.keras.layers.Flatten()(base_model.output)93 x = tf.keras.layers.Activation('relu')(x)94 x = tf.keras.layers.Dense(100)(x)95 x = tf.keras.layers.Activation('relu')(x)96 x = tf.keras.layers.Dense(classes, activation='softmax')(x)97 return tf.keras.Model(base_model.input, x)98if __name__ == '__main__':99 base = SimpleCNN.get_base_model(28,28,1)100 model = SimpleSoftmaxClassificationModel.get_classification_model(base,10)...
test.py
Source:test.py
...51 model.fit(x=x, y=y, epochs=5, batch_size=4, validation_split=0.1, verbose=0)52 def test_model_dsc(self):53 x = np.array([[5, 2, 3] * 3] * 100)54 y = np.array([[1, 2, 3] * 3] * 100)55 base_model = get_base_model(9)56 model = ModelWithCRFLossDSCLoss(base_model)57 model.compile(optimizer='adam')58 model.fit(x=x, y=y, epochs=5, batch_size=4, validation_split=0.1, verbose=0)59 def test_mixed_precison_dsc(self):60 from tensorflow.keras.mixed_precision import experimental as mixed_precision61 x = np.array([[5, 2, 3] * 3] * 100)62 y = np.array([[1, 2, 3] * 3] * 100)63 policy = mixed_precision.Policy('mixed_float16')64 mixed_precision.set_policy(policy)65 base_model = get_base_model(9)66 model = ModelWithCRFLossDSCLoss(base_model)67 model.compile(optimizer='adam')68 model.fit(x=x, y=y, epochs=5, batch_size=4, validation_split=0.1, verbose=0)69def get_model(units: int):70 base_model = get_base_model(units)71 model = ModelWithCRFLoss(base_model)72 model.compile(optimizer='adam')73 return model74def get_base_model(units: int):75 inputs = Input(shape=(None,), dtype='int32')76 output = Embedding(10, 20, trainable=True, mask_zero=True)(inputs)77 crf = CRF(units=units, dtype='float32', name='crf')78 output = crf(output)79 base_model = Model(inputs=inputs, outputs=output)80 return base_model81if __name__ == "__main__":...
test_base_model.py
Source:test_base_model.py
1import pytest2from model.base_model import BaseModel3def get_base_model(problem):4 return BaseModel("test", problem)5@pytest.mark.parametrize("problem_name, expected", [("rproblem2", [0]), ("rproblem3", [0])])6def test_model_competencies(problem_name, expected):7 base_model = get_base_model(problem_name)8 assert base_model.competencies == expected9@pytest.mark.parametrize(10 "problem_name, expected_length, expected_start",11 [("rproblem2", 1170, [8.0, 8.5, 9, 9.5]), ("rproblem3", 1176, [7.75, 8.0, 8.25, 8.5])],12)13def test_model_time_periods(problem_name, expected_length, expected_start):14 base_model = get_base_model(problem_name)15 time_periods = base_model.time_set["periods"][0]16 assert len(time_periods) == expected_length17 assert time_periods[:4] == expected_start18@pytest.mark.parametrize(19 "problem_name, expected_length, expected_start",20 [("rproblem2", 10, [8.0, 8.5, 9, 9.5]), ("rproblem3", 4, [7.75, 8.0, 8.25, 8.5])],21)22def test_model_time_periods_in_week(problem_name, expected_length, expected_start):23 base_model = get_base_model(problem_name)24 time_periods = base_model.time_set["periods"][1]25 assert len(time_periods) == expected_length26 assert time_periods[0][:4] == expected_start27def test_get_variables_returns_none():28 """ The base model should not contain any variables """29 base_model = get_base_model("problem12")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!