Skip to content

Commit

Permalink
Merge branch 'rvankoert:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
rvankoert authored Oct 25, 2023
2 parents 252165d + 18df7a5 commit e1b853c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 89 deletions.
8 changes: 4 additions & 4 deletions src/vgsl_model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ def get_model_libary() -> dict:
# "O1s92")
"model17":
("None,64,None,4 Bn Ce3,3,16 "
"RB3,3,16 "
"RBd3,3,32 "
"RBd3,3,64 "
"RBd3,3,128 "
"RB3,3,16 RB3,3,16 "
"RBd3,3,32 RB3,3,32 RB3,3,32 RB3,3,32 RB3,3,32"
"RBd3,3,64 RB3,3,64 RB3,3,64 RB3,3,64 RB3,3,64 "
"RBd3,3,128 RB3,3,128"
"Rc "
"Bl256,D20 Bl256,D20 Bl256,D20 "
"O1s92")
Expand Down
50 changes: 25 additions & 25 deletions tests/test_model_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,31 +653,31 @@ def test_bidirectional_error_handling(self):
"number of units, 'rate' is the (recurrent) dropout "
"rate.")

def test_residual_block(self):
vgsl_spec_string = "None,64,None,1 RB3,3,16 O1s10"
model_generator = self.VGSLModelGenerator(vgsl_spec_string)
model = model_generator.build()
self.assertIsInstance(model.layers[1], self.ResidualBlock)

# Layer-specific tests
self.assertEqual(model.layers[1].conv1.filters, 16)
self.assertEqual(model.layers[1].conv1.kernel_size, (3, 3))
self.assertEqual(model.layers[1].conv2.filters, 16)
self.assertEqual(model.layers[1].conv2.kernel_size, (3, 3))

# Create a model with downsampling
vgsl_spec_string = "None,64,None,1 RBd3,3,16 O1s10"
model_generator = self.VGSLModelGenerator(vgsl_spec_string)
model = model_generator.build()

# Check that the downsampling layer exists
self.assertIsInstance(model.layers[1].conv3, layers.Conv2D)
self.assertEqual(model.layers[1].conv3.filters, 16)
self.assertEqual(model.layers[1].conv3.kernel_size, (1, 1))
self.assertEqual(model.layers[1].conv3.strides, (2, 2))

# Check that conv1 also has strides of 2
self.assertEqual(model.layers[1].conv1.strides, (2, 2))
# def test_residual_block(self):
# vgsl_spec_string = "None,64,None,1 RB3,3,16 O1s10"
# model_generator = self.VGSLModelGenerator(vgsl_spec_string)
# model = model_generator.build()
# self.assertIsInstance(model.layers[1], self.ResidualBlock)
#
# # Layer-specific tests
# self.assertEqual(model.layers[1].conv1.filters, 16)
# self.assertEqual(model.layers[1].conv1.kernel_size, (3, 3))
# self.assertEqual(model.layers[1].conv2.filters, 16)
# self.assertEqual(model.layers[1].conv2.kernel_size, (3, 3))
#
# # Create a model with downsampling
# vgsl_spec_string = "None,64,None,1 RBd3,3,16 O1s10"
# model_generator = self.VGSLModelGenerator(vgsl_spec_string)
# model = model_generator.build()
#
# # Check that the downsampling layer exists
# self.assertIsInstance(model.layers[1].conv3, layers.Conv2D)
# self.assertEqual(model.layers[1].conv3.filters, 16)
# self.assertEqual(model.layers[1].conv3.kernel_size, (1, 1))
# self.assertEqual(model.layers[1].conv3.strides, (2, 2))
#
# # Check that conv1 also has strides of 2
# self.assertEqual(model.layers[1].conv1.strides, (2, 2))

def test_residual_block_error_handling(self):
# Invalid format
Expand Down
120 changes: 60 additions & 60 deletions tests/test_model_to_vgsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,66 +297,66 @@ def test_residual_block_to_vgsl(self):
self.assertEqual(
vgsl_spec, "None,64,None,1 RBd4,2,16")

def test_functional_combination_to_vgsl(self):
# Define the original model using the functional API
input_tensor = layers.Input(shape=(None, 64, 1))
x = layers.Conv2D(32, (3, 3), activation='relu')(input_tensor)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.AveragePooling2D(pool_size=(2, 2), strides=(1, 1))(x)
x = layers.BatchNormalization()(x)
x = layers.Dropout(0.25)(x)
x = self.ResidualBlock(32, 3, 3)(x)
x = layers.Reshape((-1, 1024))(x)
x = layers.LSTM(64, return_sequences=True)(x)
x = layers.GRU(64, return_sequences=True, go_backwards=True)(x)
x = layers.Bidirectional(layers.LSTM(256, return_sequences=True))(x)
x = layers.Bidirectional(layers.GRU(256, return_sequences=True))(x)
x = layers.Dense(10, activation='softmax')(x)
output_tensor = layers.Activation('linear')(x)
model = tf.keras.Model(inputs=input_tensor, outputs=output_tensor)

vgsl_spec = self.VGSLModelGenerator.model_to_vgsl(model)
self.assertEqual(
vgsl_spec, "None,64,None,1 Cr3,3,32 Mp2,2,2,2 Ap2,2,1,1 Bn D25 "
"RB3,3,32 Rc Lfs64 Grs64 Bl256 Bg256 O1s10")

# Generate a model from the VGSL string
model_generator = self.VGSLModelGenerator(vgsl_spec)
generated_model = model_generator.build()

# Compare the original model with the generated model
self.compare_model_configs(model, generated_model)

def test_sequential_combination_to_vgsl(self):
# Define the original model using the sequential API
model = tf.keras.Sequential([
layers.Input(shape=(None, 64, 1)),
layers.Conv2D(32, (3, 3), activation='relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.AveragePooling2D(pool_size=(2, 2), strides=(1, 1)),
layers.BatchNormalization(),
layers.Dropout(0.25),
self.ResidualBlock(32, 3, 3),
layers.Reshape((-1, 1024)),
layers.LSTM(64, return_sequences=True),
layers.GRU(64, return_sequences=True, go_backwards=True),
layers.Bidirectional(layers.LSTM(256, return_sequences=True)),
layers.Bidirectional(layers.GRU(256, return_sequences=True)),
layers.Dense(10, activation='softmax'),
layers.Activation('linear')
])

vgsl_spec = self.VGSLModelGenerator.model_to_vgsl(model)
self.assertEqual(
vgsl_spec, "None,64,None,1 Cr3,3,32 Mp2,2,2,2 Ap2,2,1,1 Bn D25 "
"RB3,3,32 Rc Lfs64 Grs64 Bl256 Bg256 O1s10")

# Generate a model from the VGSL string
model_generator = self.VGSLModelGenerator(vgsl_spec)
generated_model = model_generator.build()

# Compare the original model with the generated model
self.compare_model_configs(model, generated_model)
# def test_functional_combination_to_vgsl(self):
# # Define the original model using the functional API
# input_tensor = layers.Input(shape=(None, 64, 1))
# x = layers.Conv2D(32, (3, 3), activation='relu')(input_tensor)
# x = layers.MaxPooling2D(pool_size=(2, 2))(x)
# x = layers.AveragePooling2D(pool_size=(2, 2), strides=(1, 1))(x)
# x = layers.BatchNormalization()(x)
# x = layers.Dropout(0.25)(x)
# x = self.ResidualBlock(32, 3, 3)(x)
# x = layers.Reshape((-1, 1024))(x)
# x = layers.LSTM(64, return_sequences=True)(x)
# x = layers.GRU(64, return_sequences=True, go_backwards=True)(x)
# x = layers.Bidirectional(layers.LSTM(256, return_sequences=True))(x)
# x = layers.Bidirectional(layers.GRU(256, return_sequences=True))(x)
# x = layers.Dense(10, activation='softmax')(x)
# output_tensor = layers.Activation('linear')(x)
# model = tf.keras.Model(inputs=input_tensor, outputs=output_tensor)
#
# vgsl_spec = self.VGSLModelGenerator.model_to_vgsl(model)
# self.assertEqual(
# vgsl_spec, "None,64,None,1 Cr3,3,32 Mp2,2,2,2 Ap2,2,1,1 Bn D25 "
# "RB3,3,32 Rc Lfs64 Grs64 Bl256 Bg256 O1s10")
#
# # Generate a model from the VGSL string
# model_generator = self.VGSLModelGenerator(vgsl_spec)
# generated_model = model_generator.build()
#
# # Compare the original model with the generated model
# self.compare_model_configs(model, generated_model)

# def test_sequential_combination_to_vgsl(self):
# # Define the original model using the sequential API
# model = tf.keras.Sequential([
# layers.Input(shape=(None, 64, 1)),
# layers.Conv2D(32, (3, 3), activation='relu'),
# layers.MaxPooling2D(pool_size=(2, 2)),
# layers.AveragePooling2D(pool_size=(2, 2), strides=(1, 1)),
# layers.BatchNormalization(),
# layers.Dropout(0.25),
# self.ResidualBlock(32, 3, 3),
# layers.Reshape((-1, 1024)),
# layers.LSTM(64, return_sequences=True),
# layers.GRU(64, return_sequences=True, go_backwards=True),
# layers.Bidirectional(layers.LSTM(256, return_sequences=True)),
# layers.Bidirectional(layers.GRU(256, return_sequences=True)),
# layers.Dense(10, activation='softmax'),
# layers.Activation('linear')
# ])
#
# vgsl_spec = self.VGSLModelGenerator.model_to_vgsl(model)
# self.assertEqual(
# vgsl_spec, "None,64,None,1 Cr3,3,32 Mp2,2,2,2 Ap2,2,1,1 Bn D25 "
# "RB3,3,32 Rc Lfs64 Grs64 Bl256 Bg256 O1s10")
#
# # Generate a model from the VGSL string
# model_generator = self.VGSLModelGenerator(vgsl_spec)
# generated_model = model_generator.build()
#
# # Compare the original model with the generated model
# self.compare_model_configs(model, generated_model)

def compare_model_configs(self, model, generated_model):

Expand Down

0 comments on commit e1b853c

Please sign in to comment.