0

I want to run this code in the GPU

tensorflow-gpu:2.3.1

Cuda version: 10.2

Result: UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above. [[node functional_1/conv1/conv/Conv2D (defined at :129) ]] [Op:__inference_train_function_29003]

Function call stack: train_function

can you help me please ?

thank you in advance

def build_model():
#include_top: whether to include the fully-connected layer at the top of the network.
#weights: one of None (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded.
    base_model = densenet.DenseNet121(input_shape= (128, 128, 3),
                                     weights=None, 
                                     include_top=True,
                                     pooling='avg', classes=3,)
    #Layers & models also feature a boolean attribute trainable. Its value can be changed. Setting layer.trainable to False moves all the layer's weights from trainable to non-trainable. This is called "freezing" the layer: the state of a frozen layer won't be updated during training (either when
    #training with fit() or when training with any custom loop that relies on trainable_weights to apply gradient updates)
    #pour modifier les poids lors de trainement 
    for layer in base_model.layers:
        layer.trainable = True 
        
# définir les parametres nécessaires 
#La régularisation est une technique qui apporte de légères modifications à l'algorithme d'apprentissage de sorte que le 
#modèle se généralise mieux. Cela améliore également les performances du modèle sur les données invisibles.
    x = base_model.output
    x = Dense(50, kernel_regularizer=regularizers.l1_l2(0.00001), activity_regularizer=regularizers.l2(0.00001))(x)
    x = Activation('relu')(x)
    x = Dense(25, kernel_regularizer=regularizers.l1_l2(0.00001), activity_regularizer=regularizers.l2(0.00001))(x)
    x = Activation('relu')(x)
    predictions = Dense(n_classes, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    return model

model = build_model()


# l'utlisation de Adam optimizer + sparese _sparse_categorical_crossentropy
keras.optimizers.Adam(learning_rate=0.001)

#optimizer = Adam(lr=0.01)
#model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
#Early stopping is a method that allows you to specify an arbitrary large number of training epochs and stop training once the
#model performance stops improving on a hold out validation dataset
early_stop = EarlyStopping(monitor='val_loss', patience=8, verbose=2, min_delta=1e-3)
#Reduce learning rate when a metric has stopped improving.
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=4, verbose=1, min_delta=1e-3)
callbacks_list = [early_stop, reduce_lr]
print(" Build model --- %s seconds ---" % (time.time() - start_time))
print('###################### training step #############')
trainy = keras.utils.to_categorical(trainy)
yvalidation = keras.utils.to_categorical(yvalidation)


with tf.device('/device:GPU:0'):
    trainx = tf.constant(trainx)
    trainy = tf.constant(trainy)
    xvalidation = tf.constant(xvalidation)
    yvalidation = tf.constant(yvalidation)
    model_history = model.fit(trainx, trainy,
          validation_data=(xvalidation, yvalidation),
          batch_size=68, 
          epochs=7000,
          verbose=1)
Timbus Calin
  • 13,809
  • 5
  • 41
  • 59
Hela Yahyaoui
  • 29
  • 1
  • 6

1 Answers1

0

Your CuDNN+Cuda+TensorFlow versions do not match. Please consult my answer here in order to solve your problem: Tensorflow 2.0 can't use GPU, something wrong in cuDNN? :Failed to get convolution algorithm. This is probably because cuDNN failed to initialize

In your case, most probably TensorFlow 2.3.1 does not support Cuda 10.2. You should downgrade to Cuda 10.1 and try again.

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59