Bonjour,
Afin de tester mon modèle CNN, je passe celui-ci dans un script de test qui lui montre un jeu de données "Test" d'images non vues lors de l'entraînement.
Cependant, j'obtiens une précision de test de 50% soit guère mieux que l'aléatoire, voici le code dans lequel j'utilise model.predict_generator :
Code Python : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 classes = ["Normal", "Disease"] classes = np.sort(classes) nb_classes = len(classes) # Setup the VGG16 model, pretrained on ImageNet dataset, without the fully connected part. base_model = VGG16(weights='imagenet', include_top=False) # include_top=False excludes final FC layer # Add a new fully connected layer at the top of the base model. The weights of this FC layer are random # so they need to be trained model = add_new_last_layer(base_model, nb_classes) # We have already trained our model, so we just need to load it model.load_weights(weights_path) print('reading test images...') tst_generator = tst_datagen("C:\\Data\\Copy\\RECHERCHE_MAI2020\\XL-GradCAM\\20200627\\XIA\\images\\2C-DN-light5000\\test") # Get the filenames from the generator fnames = tst_generator.filenames print('FILE NAMES: ') print(fnames) # List of files in the test dataset # Get the ground truth from generator truth = tst_generator.classes print('TRUTH:') print(truth) # Get the label to class mapping from the generator label2index = tst_generator.class_indices print('INDEX:') print(label2index) # Getting the mapping from class index to class label idx2label = dict((v, k) for k, v in label2index.items()) # Get the predictions from the model using the generator print('predicting on the test images...') predictions = model.predict_generator(tst_generator,steps=tst_generator.samples / tst_generator.batch_size,verbose=0) predicted_classes = np.argmax(predictions, axis=1) print("contenu de predicted_classes : ") print(predicted_classes) errors = np.where(predicted_classes != truth)[0] print("No. of errors = {}/{}".format(len(errors), tst_generator.samples)) correct_predictions = np.where(predicted_classes == truth)[0] print("No. of correct predictions = {}/{}".format(len(correct_predictions), tst_generator.samples)) print("Test Accuracy = {0:.2f}%".format(len(correct_predictions) * 100 / tst_generator.samples))
Afin de verifier s'il n'y a pas une erreur, j'ai fait un autre script plus simple qui ouvre les images du jeu de donnée et realise une prediction que je copie dans un fichier Excel.
Là la precision est nettement plus élevée.
Voici le code où j'utilise model.predict :
Code Python : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 for PatientDirectory in os.listdir(RootPatientDirectory): FullPatientDirectory = RootPatientDirectory + PatientDirectory + '/' for root, directories, files in os.walk(FullPatientDirectory): for file in files: img_path= FullPatientDirectory + file if os.path.isfile(img_path): img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) # decode the results into a list of tuples (class, description, probability) label = classes[np.argmax(preds)] p = preds[0][np.argmax(preds)] * 100 #print("Label: {}, {:.2f}%".format(label, p), file,) #["Normal", "Disease"] if ((label == 'Normal')): label = "Normal" if ((label == 'Disease')): if p <= 85: label = "Disease" # if ((label == 'Disease') ): r = r + 1 cellule = 'A' + str(r) sheet[cellule] = PatientDirectory # Nom du répertoire correspondant a la classe # du patient cellule = 'B' + str(r) sheet[cellule] = label # Classe proposée par l'IA cellule = 'C' + str(r) sheet[cellule] = p # Probabilite cellule = 'D' + str(r) sheet[cellule] = file # Nom du l'image présentant une stenose cellule = 'F' + str(r) sheet[cellule] = FullPatientDirectory + file # Emplacement de l'image absolue (full path) # Enregistrer le fichier excel wb.save(ExcelFile)
Je ne comprends pas d'où provient cette différence, probablement dans les paramètres à passer à model.predict_generator.
Est-ce que quelqu'un pourrait m'aider ?
Partager