[Python] Different accuracy on google colab

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have this CNN:

    def cnn(trainImages, trainLabels, testImages, testLabels):
    trainImages = np.array(trainImages)
    trainLabels = np.array(trainLabels)
    testImages = np.array(testImages)
    testLabels = np.array(testLabels)

    trainImages = trainImages / 255
    testImages = testImages / 255

    model = Sequential()

    model.add(Conv2D(filters = 32, kernel_size = (3, 3), padding = 'same', activation = 'relu', input_shape = (224, 224, 3)))
    model.add(MaxPool2D(pool_size = (2, 2), strides = (2, 2)))

    model.add(Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation = 'relu'))
    model.add(MaxPool2D(pool_size = (2, 2), strides = (2, 2)))

    model.add(Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation = 'relu'))
    model.add(MaxPool2D(pool_size = (2, 2), strides = (2, 2)))

    model.add(Flatten())

    model.add(Dense(256, activation = 'relu'))
    model.add(Dense(9))

    opt = Adam(learning_rate = 0.001)

    model.compile(optimizer = opt, loss = tensorflow.keras.losses.SparseCategoricalCrossentropy(from_logits = True), metrics = ['accuracy'])

    model.fit(trainImages, trainLabels, epochs = 20, batch_size = 64)

    predictionResult = model.predict(testImages)

    pred = []
    for i in range(len(predictionResult)):
    pred.append(np.argmax(predictionResult, axis = -1))

    vehicles = ['Black Vehicles', 'Blue Vehicles', 'Brown Vehicles', 'Green Vehicles', 'Pink Vehicles', 'Red Vehicles', 'Silver Vehicles', 'White Vehicles', 'Yellow Vehicles']

    print('Accuracy: ', metrics.accuracy_score(testLabels, pred))

    print(metrics.classification_report(testLabels, pred, target_names = vehicles))

    print(metrics.confusion_matrix(testLabels, pred))



    On local I got 93%, but in Google Colab only 10%. What happens? I see that on Google Colab first training epochs have a bad accuracy 10-15% and suddenly increase to 35% in one epoch, 60% in second, over 90% in third.

    Continue reading...

Compartilhe esta Página