Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

evaluate.py 1.3 KB

You have to be logged in to leave a comment. Sign In
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
51
52
53
54
55
56
57
58
59
60
  1. import json
  2. import pandas as pd
  3. import tensorflow as tf
  4. from scripts.params import (
  5. BATCH_SIZE,
  6. DATASET_DIR,
  7. IMG_SIZE,
  8. TRAIN_DIR,
  9. EVALUATION_DIR,
  10. )
  11. #%% Create evaluation dir if necessary
  12. EVALUATION_DIR.mkdir(exist_ok=True)
  13. #%% Load test dataset
  14. test_dataset = tf.keras.preprocessing.image_dataset_from_directory(
  15. DATASET_DIR / "test",
  16. shuffle=False,
  17. batch_size=BATCH_SIZE,
  18. image_size=IMG_SIZE,
  19. )
  20. #%% Load model
  21. model = tf.keras.models.load_model(TRAIN_DIR / "model")
  22. #%%
  23. predictions = tf.nn.sigmoid(model.predict(test_dataset).flatten()).numpy()
  24. #%%
  25. predictions_df = pd.DataFrame({
  26. "image_path": test_dataset.file_paths,
  27. "prediction": predictions,
  28. }).assign(
  29. image_name=lambda df: df.image_path.str.split("/").map(lambda parts: parts[-1]),
  30. true_label=lambda df: df.image_path.str.split("/").map(lambda parts: parts[-2]),
  31. predicted_label=lambda df: (
  32. (df.prediction > 0.5)
  33. .astype(int)
  34. .map({idx: classname for idx, classname in enumerate(test_dataset.class_names)})
  35. ),
  36. )
  37. predictions_df.to_csv(EVALUATION_DIR / "predictions.csv", index=False)
  38. #%%
  39. metrics = {
  40. "test_set_length": len(predictions_df),
  41. "accuracy": (predictions_df.true_label == predictions_df.predicted_label).mean(),
  42. }
  43. with open(EVALUATION_DIR / "metrics.json", "w") as file:
  44. json.dump(metrics, file)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...