Наблюдаемое поведение:
- Без конвейера расширения слой Conv2D правильно загружает веса с помощью Layer.set_weights().
- При добавлении конвейера расширения перед слоем Conv2D Layer.set_weights() выдает ошибку, указывающую, что слой не ожидает получения весов.
- Проверка слоев модели (через print(model.layers)) явно не показывает слои расширения, но слой Conv2D, похоже, инициализируется по-другому.
data_aug = Sequential([
RandomCrop(height=384, width=384),
RandomZoom(height_factor=0.1),
RandomRotation(factor=0.0556),
Resizing(height=224, width=224)
])
# Variável x_unlabeled armazena o vetor latente da representacao criada (h-last)
feature_extraction = Sequential()
#Aplica a augmentation antes de entrar no input
feature_extraction.add(data_aug)
# Adiciona a input layer explicitamente
feature_extraction.add(Input(shape=(224, 224, 1)))
# Loop para adicionar camadas convolucionais e carregar os pesos
for k in range(n_encoder):
feature_extraction.add(Conv2D(filters[k], (filter_convolution, filter_convolution), strides=stride,
activation=activation, padding='same'))
feature_extraction.layers[-1].set_weights(autoencoder_model.layers[k + 1].get_weights())
# Add Flatten layer
feature_extraction.add(Flatten())
# Add Dense layer and set its weights
feature_extraction.add(Dense(hidden, activation=activation))
feature_extraction.layers[-1].set_weights(autoencoder_model.layers[k + 3].get_weights())
Ошибка:
Cell In[1], line 233, in Representations.Generate_all(self, stride, activation, filter_convolution, filters, output_activation, size_input_data, n_hidden, n_encoder, kernel_initializer, padding, epochs, verbose, batch_size, seeds_rep, hidden_rep, arch_rep, number_of_repr, const, techs)
229 for k in range(n_encoder):
230 feature_extraction.add(Conv2D(filters[k], (filter_convolution, filter_convolution), strides=stride,
231 activation=activation, padding='same'))
--> 233 feature_extraction.layers[-1].set_weights(autoencoder_model.layers[k + 1].get_weights())
235 # Add Flatten layer
236 feature_extraction.add(Flatten())
File c:\users\lffon\appdata\local\programs\python\python310\lib\site-packages\keras\src\layers\layer.py:696, in Layer.set_weights(self, weights)
694 layer_weights = self.weights
695 if len(layer_weights) != len(weights):
--> 696 raise ValueError(
697 f"You called `set_weights(weights)` on layer '{self.name}' "
698 f"with a weight list of length {len(weights)}, but the layer "
699 f"was expecting {len(layer_weights)} weights."
700 )
701 for variable, value in zip(layer_weights, weights):
702 if variable.shape != value.shape:
ValueError: You called `set_weights(weights)` on layer 'conv2d_6' with a weight list of length 2, but the layer was expecting 0 weights.
Подробнее здесь: https://stackoverflow.com/questions/791 ... n-pipeline