how do i pass (3,224,224,3) shape spectrograms into from tensorflow.keras.layers import…
- User
how do i pass (3,224,224,3) shape spectrograms into from tensorflow.keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D, Dense, Dropout, LSTM, Reshape, Resizing, BatchNormalization, Flatten, TimeDistributed from tensorflow.keras import layers from tensorflow.keras.applications import ResNet50V2,EfficientNetV2S, MobileNetV2, ConvNeXtTiny, ConvNeXtBase
Strategy for TPU training
regularization_parameter=0.015 dropout_value=0.5
with strategy.scope():
# Load ResNet50V2 with pre-trained ImageNet weights and modify for your task base_model = ResNet50V2(weights='imagenet', include_top=False, input_shape=(256,256, 3))base_model.trainable=False
base_model = EfficientNetV2M(weights='imagenet', include_top=False, input_shape=(256, 256, 3))
# Add custom layers x = base_model.output# # Optional: Add Conv2D layers after the base model
x = Conv2D(256, kernel_size=(3, 3), activation='relu', padding='same', kernel_regularizer=l2(regularization_parameter))(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(dropout_value)(x)
# Add another Conv2D layer
x = Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same', kernel_regularizer=l2(regularization_parameter))(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(dropout_value)(x)
x = GlobalAveragePooling2D()(x) # Use GlobalAveragePooling2D to reduce dimensionality
x = Flatten()(x) # Flatten the convolutional feature maps to a 1D vector # Reshape for LSTM input (we add a 'temporal' dimension) x = layers.Reshape((1, -1))(x) # Reshape to (batch_size, time_steps=1, features) # Add LSTM Layerx = LSTM(256, return_sequences=True)(x) # First LSTM with 256 units
x = LSTM(64, return_sequences=False)(x) # LSTM with 64 units # Increase regularization with more dropout and L2x = Dense(1024, activation='relu', kernel_regularizer=l2(regularization_parameter))(x)
# x = BatchNormalization()(x)
x = Dropout(dropout_value)(x)
x = Dense(512, activation='relu', kernel_regularizer=l2(regularization_parameter))(x)x = BatchNormalization()(x)
x = Dropout(dropout_value)(x) x = Dense(256, activation='relu', kernel_regularizer=l2(regularization_parameter))(x)x = BatchNormalization()(x)
x = Dropout(dropout_value)(x) x = Dense(128, activation='relu', kernel_regularizer=l2(regularization_parameter))(x)x = BatchNormalization()(x)
x = Dropout(dropout_value)(x) # Output layers for age and gender classification" age_output = Dense(3, activation='softmax', name='age')(x) gender_output = Dense(1, activation='sigmoid', name='gender')(x) # Learning Rate Decay reduce_lr = tf.keras.callbacks.ReduceLROnPlateau( monitor='val_loss', # Metric to monitor factor=0.1, # Factor by which to reduce the learning rate patience=3, # Number of epochs with no improvement after which the learning rate is reduced min_lr=1e-10 # Lower bound on the learning rate ) # Early Stopping early_stopping = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True) # Create the final model model = Model(inputs=base_model.input, outputs=[age_output, gender_output]) # Compile the model with appropriate loss functions model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3), loss={'age': 'categorical_crossentropy', 'gender': 'binary_crossentropy'}, metrics={'age': 'accuracy', 'gender': 'accuracy'}) - Assistant