-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrGuessCarsBrandsKaggle_Inception_v3_1_49.py
170 lines (116 loc) · 4.55 KB
/
lrGuessCarsBrandsKaggle_Inception_v3_1_49.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
"""
Alfonso Blanco García , Jun 2023
"""
######################################################################
# PARAMETERS
######################################################################
######################################################################
import os
import re
import cv2
import numpy as np
import keras
import functools
import time
inicio=time.time()
TabCarBrand=[]
f=open("CarBrand.csv","r")
for linea in f:
lineadelTrain =linea.split(",")
TabCarBrand.append(lineadelTrain[3])
def GetBrandFromModel(Modelo):
f=open("CarBrand.csv","r")
for linea in f:
lineadelTrain =linea.split(",")
ModelFrom=int(lineadelTrain[1])
ModelTo=int(lineadelTrain[2])
if Modelo >= ModelFrom and Modelo <= ModelTo:
return int(lineadelTrain[0]), lineadelTrain[3]
print("RARO NO ENCUENTRA EL MODELO")
return -1, ""
def loadimagesTest():
images=[]
Y=[]
imagesName=[]
f=open("cardatasettrain.csv","r")
ContTraining=0
ContValid=0
Conta=0;
for linea in f:
Conta=Conta+1
if Conta==1: continue
if Conta < 8000: continue
lineadelTrain =linea.split(",")
NameImg=lineadelTrain[6]
# OJO LLEVA UN CR AL FINAL
NameImg=NameImg[0:9]
img=cv2.imread('C:\\archiveKaggle\\cars_train\\cars_train' + '\\'+str(NameImg))
img = cv2.resize(img, (224,224), interpolation = cv2.INTER_AREA)
Modelo=int(lineadelTrain[5])
Brand, BrandName=GetBrandFromModel(Modelo)
if Brand==-1 :
print ("NO SE ENCUENTRA MODELO " + str(Modelo) + " en " + NameImg)
#if Brand >20: continue
Y.append(Brand)
images.append(img)
imagesName.append(NameImg)
return images, Y, imagesName
def PredictModel(model,x_test_test):
predictions1=model.predict(x_test_test)
#print(predictions1)
predictions=np.argmax(predictions1, axis=1)
#print(predictions)
p=[]
p.append(predictions1[0][predictions])
return predictions, p, predictions1
###########################################################
# MAIN
##########################################################
from tensorflow.keras.models import load_model
#model_1_10 = load_model('lrbest_brand_1_49.h5')
model_1_10 = load_model('lrModelCarsBrands_Inception_v3_1_49.h5')
X_test, Y_test, imageName_test = loadimagesTest()
x_test=np.array(X_test)
# Scale images to the [0, 1] range
x_test = x_test.astype("float32") / 255.0
TotalHits=0
TotalFailures=0
with open( "BrandsResults.txt" ,"w") as w:
for i in range(len(x_test)):
TabP=[]
TabModel=[]
TabPredictions1=[]
x_test_test=[]
x_test_test.append(x_test[i])
x_test_test=np.array(x_test_test)
predictions, p, predictions1 = PredictModel(model_1_10,x_test_test)
predictions=int(predictions[0])
predictions=predictions+1
IndexCarBrandPredict=predictions-1
IndexCarBrandTrue=Y_test[i]-1
NameCarBrandPredict=TabCarBrand[IndexCarBrandPredict]
NameCarBrandTrue=TabCarBrand[IndexCarBrandTrue]
if Y_test[i]!=predictions:
TotalFailures=TotalFailures + 1
print("ERROR " + imageName_test[i]+ " is assigned brand " + str(predictions)
+ NameCarBrandPredict + " True brand is " + str(Y_test[i])+ NameCarBrandTrue)
else:
print("HIT " + imageName_test[i]+ " is assigned brand " + str(predictions)
+ NameCarBrandPredict )
# + "probabilidad = " + str(Pmax))
#print("probabilidad = " + str(p[0]))
TotalHits=TotalHits+1
lineaw=[]
lineaw.append(imageName_test[i])
lineaw.append(str(Y_test[i]))
lineaw.append(NameCarBrandTrue)
lineaw.append(NameCarBrandPredict)
lineaw.append( str(p))
lineaWrite =','.join(lineaw)
lineaWrite=lineaWrite + "\n"
w.write(lineaWrite)
print("")
print("Total hits = " + str(TotalHits))
print("Total failures = " + str(TotalFailures) )
print("Accuracy = " + str(TotalHits*100/(TotalHits + TotalFailures)) + "%")