-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFca.py
More file actions
executable file
·143 lines (116 loc) · 4.04 KB
/
RFca.py
File metadata and controls
executable file
·143 lines (116 loc) · 4.04 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 13 09:14:18 2022
@author: alessandro
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#import cv2
import os
from contactangleprediction.contactangles import *
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('path', type=str, help='input the path')
parser.add_argument('imageformat', type=str, default=".png", help='input the image format')
args = parser.parse_args()
imageformat = args.imageformat
path = args.path
#imageformat = ".png"
#path = '/20x20/'
imfilelist = [os.path.join(path,f) for f in os.listdir(path) if f.endswith(imageformat)]
imfilelist.sort()
contact_angles = [x for x in np.linspace(1,179,179)]
del contact_angles[89]
def replicate(List, n):
return List*n
contact_angles = replicate(contact_angles,28)
contact_angles = pd.DataFrame(data =contact_angles, columns=['Contact Angles'])
contact_angles= contact_angles/180
x = np.linspace(1,179,179)
x = np.delete(x,89)
y = np.arange(7)
array1 = []
for i in range(len(y)):
for j in range(len(x)):
z = ["lines%01d%03d" %(y[i] , x[j])+".png" ]
array1.append(z)
array2 = []
for i in range(3):
for j in range(1246):
zz = ["blur%01d%04d" %(i,j)+".png" ]
array2.append(zz)
array = array1 +array2
#array = ["lines%01d%03d" %(y[9] , x[45])+".png" ]
data_x = pd.DataFrame(data=array, columns=['Filename'],dtype=str)
my_data = pd.concat([data_x,contact_angles],axis=1) # axis = 1 means columns
# Importing the dataset
imageformat = ".png"
imfilelist = [os.path.join(path,f) for f in os.listdir(path) if f.endswith(imageformat)]
imfilelist.sort()
from keras.preprocessing import image
X = []
for IMG in imfilelist:
ima = image.load_img(IMG, target_size=(20,20), color_mode='grayscale')
ima = image.img_to_array(ima)
ima /= 255
ima = ima.reshape((400))
X.append(ima)
X = np.array(X).reshape(-1,400)
y = my_data.iloc[:, -1].values
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1)
# Training the Random Forest Regression model on the whole dataset
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)
regressor.fit(X_train, y_train)
# Predicting the Test set results
y_pred = regressor.predict(X_test)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
# Evaluating the Model Performance
from sklearn.metrics import r2_score
r2_score(y_test, y_pred)
# =============================================================================
# img = image.load_img('/crop0137.png', target_size=(20,20,1), color_mode='grayscale') #20,44,121,190
# img = image.img_to_array(img)
# img = img /255
# img = img.reshape(1,400)
# prediction = regressor.predict(img)
# print(prediction*180)
# =============================================================================
from tqdm import tqdm
#import contact_angles
imageformat = ".png"
path = '/error_analysis/1200/'
imfilelist = [os.path.join(path,f) for f in os.listdir(path) if f.endswith(imageformat)]
imfilelist.sort()
from keras.preprocessing import image
folder = []
for IMG in imfilelist:
ima = image.load_img(IMG, target_size=(20,20), color_mode='grayscale')
ima = image.img_to_array(ima)
ima /= 255
ima = ima.reshape(1,400)
folder.append(ima)
# RF Prediction
contact_angle = []
trend_err = np.zeros([len(folder)])
for img in tqdm(folder):
prediction = regressor.predict(img)
contact_angle.append(prediction*180)
arr = [contact_angles_ellipse]
error = np.zeros(len(folder))
for i in range(len(folder)):
#error[i] = abs((contact_angle[i]-arr[0][i])/arr[0][i])
error[i] = abs((contact_angle[i]-arr[0][i]))
cnn_measurment = list(zip(np.array(arr[0]), error))
av_err = sum(list(error))/len(error)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.array(arr[0]),error,'co-')
ax.set_xlabel('Angles')
ax.set_ylabel('Error')
plt.show()