Advertisement
Void-voiD

Untitled

Jun 22nd, 2023
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | Source Code | 0 0
  1. import os
  2.  
  3. import numpy as np
  4. import pandas as pd
  5. from keras.models import load_model
  6. import tensorflow as tf
  7.  
  8. from custom_keras_utils import normalize_timeseries
  9. from preprocess_test_samples import load_data_to_datasets
  10.  
  11.  
  12. ROOT_PATH = os.getcwd() + '\\test_samples_0'
  13. CLASS_NAME = 'Расслоение'
  14. CLASS_MAPPING = {
  15.     'ВИ_БезДефекта': 0,
  16.     'ВИ_Дефект': 1,
  17.     'Инородное включение': 2,
  18.     'Непроклей': 3,
  19.     'Непропитка': 4,
  20.     'Пресс_БезДефекта': 5,
  21.     'Пресс_Дефект': 6,
  22.     'Расслоение': 7
  23. }
  24. MODEL_NAME = 'resnet'
  25. MODEL_MAPPING = {
  26.     'lstm_fcn': 1,
  27.     'mlp': 2,
  28.     'resnet': 3
  29. }
  30.  
  31.  
  32. with tf.device('/GPU:0'):
  33.     model = load_model(MODEL_NAME + '.h5')
  34.     load_data_to_datasets(ROOT_PATH)
  35.  
  36.     X_test = pd.read_csv(ROOT_PATH + '\\test_x.csv', header=None, delimiter=' ')
  37.     X_test = normalize_timeseries(X_test).values
  38.     if MODEL_MAPPING[MODEL_NAME] == 3:
  39.         X_test = np.reshape(X_test, (X_test.shape[0], 1, 1, X_test.shape[1]))
  40.     else:
  41.         X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
  42.  
  43.     # Make predictions
  44.     predictions = model.predict(X_test)
  45.  
  46.     # Convert predictions to class labels
  47.     class_labels = [np.argmax(prediction) for prediction in predictions]
  48.  
  49.     false_predict = 0.0
  50.     all_predict = 0
  51.     for label in class_labels:
  52.         if label != CLASS_MAPPING[CLASS_NAME]:
  53.             false_predict += 1
  54.         all_predict += 1
  55.     print('Accuracy: {}%'.format((all_predict - false_predict) / all_predict * 100))
  56.  
  57.     os.remove(ROOT_PATH + '\\test_x.csv')
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement