Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

How to associate each input image names with its predictions while exporting results to .csv file in python

I have exported the results of the below code to .csv as below. pd.DataFrame(predictions, columns= ['triangles','circles','squares']).to_csv('predictions.csv')

But I would only get prediction scores for each class.

I need the results in format = (Image_ID, Image_name, confidence level, its class name). How do I include other details in the output ?

Below is the complete code.

import os
path= 'C:\Users\Chethan\Documents\GreatLakes\Computer Vision\Week 
1\CNN_Basics\Shapes\shapes'
os.chdir(path)
%matplotlib inline
import glob
from keras.layers import Dense
from keras.layers import Dropout
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
import pandas as pd
import seaborn as sns

print(os.listdir())
print(os.getcwd())

t_path = path +"/squares/drawing(1).png"  #saving the path for the example 
1 in the triangles folder
print(os.path.exists(t_path))   #checking whether the path exists or not 

#Trying to load the image and display it 
t_path = path +"/squares/drawing(1).png"
img = mpimg.imread(t_path)
print(img.shape)  
imgplot = plt.imshow(img)
plt.show()

#Trying to load the image and display it 
t_path = path +"/squares/drawing(1).png"
img = mpimg.imread(t_path)
#when it reads image it reads as 3 dimensional tensor each point indicates 
3 dimensions value and it grows to hight then it append each width column
def rgb2gray(img):
return np.dot(img, [0.299, 0.587, 0.114])

gray = rgb2gray(img)
#print(type(gray))
#print(gray.shape)
imgplot = plt.imshow(gray,cmap = 'gray')
plt.show()     
img = np.ravel(gray)
#print(type(img))   #image type is numpy.ndarray
print(img.shape)  
#img1 = np.ravel(img)
#use classical techniques instead of using CNN
df=pd.DataFrame([img])
shapes = ['triangles','circles','squares']

files=[]
result=[]
path1 = "C:\Users\Chethan\Documents\GreatLakes\Computer Vision\Week 
1\CNN_Basics\Shapes\shapes\" 
files = [] #for storing all the images path 
result = []
for shape in shapes:                                       #|
     new_path = path1 + shape                               #|
     for file in os.listdir(new_path):                     #
        files.append(os.path.join(new_path,file))            #|
        result.append(shape)
#You can run this to check the values
print(len(files))
print(files[1])
#print(len(result))

#Now we have all the images in a list In the below section we will process 
each image and try to save it our DataFrame 
images = [] #list for images  
for file in files:
    img = mpimg.imread(file)
    img = rgb2gray(img)
    img = np.ravel(img)
    images.append(img)

#print(len(images))
#print(len(images[0]))
df = pd.DataFrame(images[1:200])   #converting our images list into Pandas 
DataFrame
#a = np.array(images)
df.head()
#df.shape
#df.describe()
#We have to add one column of labels of the images i.e. result column
df.loc[:,'result']=result[1:200]
df.tail()
#Now we will prepare our dataset for training
#0 --> triangles 1 ---> circles 2 ---> squares
temp_df = df
temp_df.head()
#changing the result column data into numerical categories
temp_df.loc[:,'result'] = pd.factorize(temp_df.result)[0]
temp_df.head()

sns.countplot(temp_df['result'])

#Now we need to shuffle our DataFrame so that we can split our dataset 
into two dataset One for training our model and other for testing our 
model
from sklearn.utils import shuffle
#Now shuffling the rows in the dataframe
df = shuffle(temp_df)
df.head()
df.tail()
y=df.result
X=df.drop(['result'], axis=1)

from sklearn.model_selection import train_test_split  #for splitting our 
dataframe into training and test example
#Now splitting our dataset in train and test 
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 
0.2,random_state = 0)
y_test.astype('int32')
y_test.shape

#Convert back to tensors to use NN models 
#applying one_hot encoding in the y
import numpy as np
from keras.utils.np_utils import to_categorical #convert to one_hot 
encoding
y = to_categorical(y,num_classes = 3)
y = y.astype('int32')
X1 = X.values.reshape(-1,28,28,1)

print(X1.shape)
print(y.shape)

from sklearn.model_selection import train_test_split  #for splitting our 
dataframe into training and test example
#Now splitting our dataset in train and test 
X_train,X_test,y_train,y_test = train_test_split(X1,y,test_size = 
0.2,random_state = 0)
y_test.astype('int32')
y_test.shape

# define model
import keras
from keras import losses
from keras import optimizers
from keras.layers import Dropout, MaxPooling2D
from keras.layers import BatchNormalization, Dropout
num_classes=3

# Load the pre trained model from the HDF5 file saved previously
pretrained_model = 


keras.models.load_model('C:\Users\Chethan\Documents\
GreatLakes\Computer Vision\Week 
1\CNN_Basics\Shapes\shapes\classifier.h5')
pretrained_model.load_weights('C:\Users\Chethan\
Documents\GreatLakes\Computer Vision\Week 
1\CNN_Basics\Shapes\shapes\classifier_weights.h5')

# There are 3823 training images and 500 test images in total
pretrained_model.fit(X_train,y_train,validation_data=(X_test, 
y_test),batch_size=40,epochs = 20)

#validation_data=(X_test, y_test)
#steps_per_epoch = 10,
#validation_steps = int(20)

# Final evaluation of the model
scores = pretrained_model.evaluate(X_test, y_test, verbose=0)
print("Error: %.2f%%" % (100-scores[1]*100))
pretrained_model.summary()

pred = pretrained_model.predict(X_test)

pd.DataFrame(predictions, columns= 
['triangles','circles','squares']).to_csv('predictions.csv') 
question from:https://stackoverflow.com/questions/65941639/how-to-associate-each-input-image-names-with-its-predictions-while-exporting-res

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...