|
import os |
|
import csv |
|
import numpy as np |
|
from tensorflow.keras.preprocessing import image |
|
import tensorflow as tf |
|
|
|
predict_dir = "/Users/rosh/Downloads/Eval_data" |
|
model = tf.keras.models.load_model("model_4_improved_8.h5") |
|
|
|
class_labels = ['Crane', 'Crow', 'Egret', 'Kingfisher','Myna','Peacock','Pitta','Rosefinch','Tailorbird','Wagtail'] |
|
|
|
with open('pred.csv', mode='w', newline='') as csvfile: |
|
writer = csv.writer(csvfile) |
|
writer.writerow(['Name', 'Target_name','Target_num']) |
|
qq=0 |
|
|
|
for img_file in os.listdir(predict_dir): |
|
print(qq) |
|
|
|
img_path = '/Users/rosh/Downloads/Eval_data'+'/'+ img_file |
|
img = image.load_img(img_path, target_size=(224, 224)) |
|
|
|
|
|
img_array = image.img_to_array(img) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
prediction = model.predict(img_array,verbose=0) |
|
predicted_class = np.argmax(prediction, axis=1)[0] |
|
|
|
class_indices = [0,1,2,3,4,5,6,7,8,9] |
|
class_names =['Crane', 'Crow', 'Egret', 'Kingfisher', 'Myna', 'Peacock', 'Pitta', 'Rosefinch', 'Tailorbird', 'Wagtail'] |
|
predicted_class_name = class_names[predicted_class] |
|
|
|
writer.writerow([img_file[:img_file.index('.jpg')], predicted_class_name,predicted_class]) |
|
qq+=1 |
|
|
|
print("Predictions saved to predictions.csv") |