-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogohunter.cs
More file actions
266 lines (243 loc) · 10.8 KB
/
Copy pathLogohunter.cs
File metadata and controls
266 lines (243 loc) · 10.8 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Alturos.Yolo;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Markup;
using TensorFlow;
namespace Logohunter_cshap
{
class Logohunter
{
private YoloWrapper _yolowrapper;
private TFGraph _graph;
private Dictionary<string, double> _cutoffs;
private const string CFG = "yolo_logohunter.cfg";
private const string WEIGHTS = "yolo_logohunter.weights";
private const string NAMES = "yolo_logohunter.names";
private const string INCEPTION = "inception_logohunter.pb";
//possible values flickr27_features.csv or logosinthewild_features.csv
private const string FEATURES = "flickr27_features.csv";
public Logohunter(List<string> brandsPaths)
{
_yolowrapper = new YoloWrapper(CFG, WEIGHTS, NAMES);
_graph = new TFGraph();
_graph.Import(File.ReadAllBytes(INCEPTION));
_cutoffs = LoadBrandsComputeCutoffs(brandsPaths, LoadFeatures());
}
public Logohunter(string brandsFolder)
{
_yolowrapper = new YoloWrapper(CFG, WEIGHTS, NAMES);
_graph = new TFGraph();
_graph.Import(File.ReadAllBytes(INCEPTION));
List<string> brandsPaths = Directory.GetFiles(brandsFolder).ToList();
_cutoffs = LoadBrandsComputeCutoffs(brandsPaths, LoadFeatures());
}
public void RunDetection(List<string> imagePaths)
{
foreach (string imagePath in imagePaths)
{
var candidates = _yolowrapper.Detect(imagePath);
foreach (var candidate in candidates)
{
Bitmap bmp = ImageUtil.CropImage(new Bitmap(imagePath), candidate.X, candidate.Y, candidate.Width, candidate.Height);
float[] candidateFeatures = ExtractFeatures(bmp);
foreach(var brand in _cutoffs)
{
var similarity = ComputeCosineSimilatity(candidateFeatures, ExtractFeatures(new Bitmap(brand.Key)));
if(similarity > brand.Value)
{
Random random = new Random();
bmp.Save($@"data\results\{Path.GetFileNameWithoutExtension(brand.Key)}_{random.Next()}.jpg");
}
}
}
}
}
public void RunDetection(string imagesFolder)
{
List<string> imagePaths = Directory.GetFiles(imagesFolder).ToList();
foreach (string imagePath in imagePaths)
{
var candidates = _yolowrapper.Detect(imagePath);
foreach (var candidate in candidates)
{
Bitmap bmp = ImageUtil.CropImage(new Bitmap(imagePath), candidate.X, candidate.Y, candidate.Width, candidate.Height);
float[] candidateFeatures = ExtractFeatures(bmp);
double maxSimilarity = 0;
string maxSimilarBrand = "";
foreach (var brand in _cutoffs)
{
var similarity = ComputeCosineSimilatity(candidateFeatures, ExtractFeatures(new Bitmap(brand.Key)));
if (similarity > brand.Value && (similarity - brand.Value) > maxSimilarity)
{
maxSimilarity = similarity;
maxSimilarBrand = brand.Key;
}
}
if (maxSimilarity != 0)
{
Random random = new Random();
bmp.Save($@"data\results\{Path.GetFileNameWithoutExtension(maxSimilarBrand)}_{random.Next()}.jpg");
}
}
}
}
private float[] ExtractFeatures(Bitmap candidate)
{
var tensor = ImageUtil.CreateTensorFromBitmap(candidate);
var session = new TFSession(_graph);
var runner = session.GetRunner();
runner.AddInput(_graph["input_2"][0], tensor);
runner.Fetch(_graph["mixed8/concat"][0]);
var output = runner.Run();
TFTensor result = output[0];
var features = (float[,,,])result.GetValue(jagged: false);
//features shape: [1,4,4,1280]
float[] flattenFeatures = new float[20480];
int n = 0;
for (int k = 0; k < 1280; k++)
{
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 4; i++)
{
flattenFeatures[n] = features[0, j, i, k];
n++;
}
}
}
return flattenFeatures;
}
private float[] ExtractFeatures(string imagePath)
{
var tensor = ImageUtil.CreateTensorFromImageFile(imagePath);
var session = new TFSession(_graph);
var runner = session.GetRunner();
runner.AddInput(_graph["input_2"][0], tensor);
runner.Fetch(_graph["mixed8/concat"][0]);
var output = runner.Run();
TFTensor result = output[0];
var features = (float[,,,])result.GetValue(jagged: false);
float[] flattenFeatures = new float[20480];
int n = 0;
for (int k = 0; k < 1280; k++)
{
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 4; i++)
{
flattenFeatures[n] = features[0, j, i, k];
n++;
}
}
}
return flattenFeatures;
}
private List<float[]> LoadFeatures()
{
List<float[]> features = new List<float[]>();
using (var reader = new StreamReader(FEATURES))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split('\t');
if(values.Length != 20480)
{
values = line.Split(',');
}
float[] vector = new float[values.Length];
for (int i = 0; i < vector.Length; i++)
{
if (string.IsNullOrEmpty(values[i])){
vector[i] = 0;
}
else {
if(FEATURES == "logosinthewild_features.csv")
{
vector[i] = float.Parse(values[i], NumberStyles.Number | NumberStyles.AllowExponent, new CultureInfo("en-US"));
}
else
{
vector[i] = float.Parse(values[i], CultureInfo.InvariantCulture);
}
}
}
features.Add(vector);
Console.WriteLine($"Feature readed: {features.Count}");
}
}
return features;
}
private Dictionary<string, double> LoadBrandsComputeCutoffs(List<string> brands, List<float[]> featuresList)
{
Dictionary<string, double> cutoffs = new Dictionary<string, double>();
for (int i = 0; i < brands.Count; i++)
{
var brandFeatures = ExtractFeatures(brands[i]);
//to compute 95% cutoff of similarity distibution we save only top 5% values of similarity and choose min of them.
double[] topFivePercentSimilarity = new double[(int)(featuresList.Count*0.05)];
foreach (float[] features in featuresList)
{
double similarity = ComputeCosineSimilatity(brandFeatures, features);
if (similarity > topFivePercentSimilarity[0])
{
topFivePercentSimilarity[0] = similarity;
topFivePercentSimilarity = topFivePercentSimilarity.OrderBy(x => x).ToArray();
}
}
cutoffs.Add(brands[i], topFivePercentSimilarity[0]);
}
return cutoffs;
}
private double ComputeCosineSimilatity(float[] brandFeatures, float[] features)
{
double sum = 0;
double squareBrand = 0;
double squareFeatures = 0;
for(int i = 0; i < 20480; i++)
{
sum += brandFeatures[i] * features[i];
squareBrand += brandFeatures[i] * brandFeatures[i];
squareFeatures += features[i] * features[i];
}
return sum / (Math.Sqrt(squareBrand) * Math.Sqrt(squareFeatures));
}
/// <summary>
/// Computes logo features of LogosInTheWildv2 dataset for extractor model
/// </summary>
/// <param name="datasetFolder">Folder with all logos images.</param>
/// <param name="featuresFile">Output features file name.</param>
public void CreateFeatures(string datasetFolder, string Rois, string featuresFile)
{
var file = File.CreateText(featuresFile);
List<float[]> features = new List<float[]>();
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
using (var reader = new StreamReader(Rois))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(' ');
var logo = ImageUtil.CropImage(
Image.FromFile(@"flickr_logos_27_dataset_images\" + values[0]),
int.Parse(values[3]),
int.Parse(values[4]),
int.Parse(values[5]) - int.Parse(values[3]),
int.Parse(values[6]) - int.Parse(values[4]));
var feature = ExtractFeatures(logo);
features.Add(feature);
file.WriteLine(string.Join("\t", feature));
Console.WriteLine($"Feature created: {features.Count}");
}
}
file.Close();
}
}
}