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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
- import keras
- import tensorflow as tf
- from tensorflow.keras.preprocessing.image import random_rotation,random_shift,random_brightness
- from tensorflow.keras import layers
- from tensorflow.keras.models import Model
- from tensorflow.keras.models import Sequential
- from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, GlobalMaxPooling2D,Dropout,GlobalAveragePooling2D, BatchNormalization
- from tensorflow.keras.callbacks import LearningRateScheduler, EarlyStopping, Callback
- from tensorflow.keras.models import Sequential, load_model
- from tensorflow.keras.callbacks import ModelCheckpoint
- from tensorflow.keras.optimizers import Adam
- from sklearn.metrics import confusion_matrix, classification_report,precision_score,recall_score,accuracy_score
- import cv2 as cv
- from dagshub.streaming import DagsHubFilesystem
- import mlflow
- from dagshub_config import *
- from config import *
- import json
- import numpy as np
- import pandas as pd
- import os
- import sys
- import git
- import pathlib
- import math
- import random
- from scipy.stats import entropy
- random.seed(20)
- from operator import itemgetter
- from dagshub.upload import Repo
- import argparse
- INITIAL_TRAINING_SAMPLES=400
- NUM_SAMPLES_LABELLED_PER_ITERATION=100
- MAX_SAMPLES_LABELLED=6000
- POOL_SIZE=4000
- mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
- os.environ['MLFLOW_TRACKING_URI']=MLFLOW_TRACKING_URI
- os.environ["MLFLOW_TRACKING_USERNAME"] = MLFLOW_TRACKING_USERNAME
- os.environ["MLFLOW_TRACKING_PASSWORD"] = MLFLOW_TRACKING_PASSWORD
- print(mlflow.get_tracking_uri())
- def create_train_data_frame(train_ok_data_path,train_defect_data_path):
- fs=create_streaming_client()
- train_data=pd.DataFrame()
- ok_images=fs.listdir(train_ok_data_path)
- defect_images=fs.listdir(train_defect_data_path)
- images_path=ok_images+defect_images
- train_data['file_name']=images_path
- train_data['labelled']=False
- return train_data
- def label_image(file_name,is_to_be_labelled):
- if is_to_be_labelled==True:
- if "ok" in file_name:
- return "ok"
- else:
- return "defect"
- return ""
- def predict(classifier,image):
- image_dim=image.shape
- #print(image_dim)
- X = np.empty((1,*image_dim))
- X[0,]=image
- #print(X.shape)
- return classifier.predict(X)[0]
- def generate_clean_images(folder_path,image_dim,n_channels):
-
- '''
- This function reads the images in the folder_path and cleans them
- The output is the file_path and the cleaned numpy array of the image
- '''
- fs=create_streaming_client()
- files=fs.listdir(folder_path)
- file_paths=[os.path.join(folder_path,__file__) for __file__ in files]
- images={file_path:clean_image(fs,file_path,image_dim,n_channels) for file_path in file_paths}
- return images
- def predict_test_data(unlabelled_images,cleaned_images,classifier):
- predicted_probability={file_path:predict(classifier,image_array) for file_path,image_array in cleaned_images.items() if os.path.basename(file_path) in unlabelled_images}
- print("Predicted Prob Dictionary Length ",len(predicted_probability))
- return predicted_probability
- def random_query_strategy(unlabelled_images,query_size):
- '''
- In random strategy, we will pick random samples from the unlabelled data and label them.
- We will then add this to the pool of training data and retrain the model
-
- '''
- images_to_label=random.sample(unlabelled_images,query_size)
- return images_to_label
- def entropy_query_strategy(unlabelled_images,query_size,classifier,cleaned_images=None):
- '''
- This function,gets the predicted probability of unlabelled images and returns the images with the highest entropy for training
- '''
-
- image_pred_prob=predict_test_data(unlabelled_images,cleaned_images,classifier)
- #print(image_pred_prob)
- image_pred_entropy={os.path.basename(file_path):entropy([prob,1-prob]) for file_path,prob in image_pred_prob.items()}
- #print(image_pred_entropy)
- ### Get the top N images with the highest entropy
- top_entropy = dict(sorted(image_pred_entropy.items(), key = itemgetter(1), reverse = True)[:query_size])
- print("Images to Label ",top_entropy.keys())
- return list(top_entropy.keys())
-
- def create_streaming_client():
- fs = DagsHubFilesystem(project_root=DAGSHUB_REPO_NAME,username=DAGSHUB_USERNAME,password=DAGSHUB_TOKEN)
- return fs
- def convert_to_grayscale(image):
- converted=tf.image.hsv_to_rgb(image)
- converted=tf.image.rgb_to_grayscale(converted)
- return converted
- def read_images_streaming(fs,img_path):
- fs.open(img_path) ## This does smart caching
- return cv.imread(img_path)
- def get_experiment_id(name):
- exp = mlflow.get_experiment_by_name(name)
- if exp is None:
- exp_id = mlflow.create_experiment(name)
- return exp_id
- return exp.experiment_id
- '''
- This data generator reads the data from a given path and generates batches of data. This is the generator used for Generating Batches of Test Data
- '''
- class DataGenerator(keras.utils.Sequence):
- 'Generates data for Keras'
-
- def __init__(self,data_directory,mode='train', image_cls={'ok_front':0,'def_front':1},
- batch_size=32, dim=(150, 150), n_channels=3, shuffle=True,train_test_split=0.8,
- rotation_range=None, width_shift_range=None, height_shift_range=None,horizontal_flip=False,brightness_range=None):
- """
- Initialise the data generator
- """
- self.dim = dim
- self.batch_size = batch_size
- self.labels = {}
- self.list_IDs = []
- self.rotation_range=rotation_range
- self.horizontal_flip=horizontal_flip
- self.brightness_range=brightness_range
- self.height_shift_range=height_shift_range
- self.width_shift_range=width_shift_range
-
- self.fs=create_streaming_client()
-
- # glob through directory of each class
- label_class=[key for key,val in image_cls.items()]
- for i, class_name in enumerate(label_class):
- ### Get the number of images in both classes and split the data into training and validation
- num_images=len(self.fs.listdir(os.path.join(data_directory,class_name+"/")))
- print("number of images in "+class_name+" is "+str(num_images))
- #paths = glob.glob(os.path.join(TRAIN_DATASET_PATH, cls, '*'))
- brk_point = int(num_images*train_test_split) #Divide the data into 80:20 - training and validation set
-
- file_paths=self.fs.listdir(os.path.join(data_directory,class_name+"/"))
- folder_path=os.path.join(data_directory,class_name+"/")
- if mode in ['train',"test"]:
- file_paths = file_paths[:brk_point]
-
- else:
- file_paths = file_paths[brk_point:]
-
- file_paths=[os.path.join(folder_path,file_path) for file_path in file_paths]
- self.list_IDs += file_paths
- self.labels.update({p:image_cls[class_name] for p in file_paths})
- print("Number of Files For "+mode+"for class "+class_name+" is "+str(len(file_paths)))
-
- self.n_channels = n_channels
- self.n_classes = len(label_class)
- self.sample_size=len(self.list_IDs)
- self.shuffle = shuffle
- self.on_epoch_end()
-
- self.on_epoch_end()
- def __len__(self):
- 'Denotes the number of batches per epoch'
- return int(np.floor(len(self.list_IDs) / self.batch_size))
- def __getitem__(self, index):
- 'Generate one batch of data'
- # Generate indexes of the batch
- indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
- # Find list of IDs
- #list_IDs_temp = [self.list_IDs[k] for k in indexes]
- # Generate data
- X, y = self.__data_generation(indexes)
- return X, y
- def on_epoch_end(self):
- 'Updates indexes after each epoch. If shuffle is true, this will shuffle the dataset'
- print("In On_EPOCH_END")
- self.indexes = np.arange(len(self.list_IDs))
- if self.shuffle == True:
- np.random.shuffle(self.indexes)
-
- def __data_generation(self, indexes):
-
- 'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
- # Initialization
- X = np.empty((self.batch_size, *self.dim, self.n_channels))
- y = np.empty((self.batch_size), dtype=int)
-
- ### Generate data based on the indexes.
- for i,idx in enumerate(indexes):
-
- img_path=self.list_IDs[idx]
- ### Read the Images using Streaming Client
- img=read_images_streaming(self.fs,img_path)
-
-
- img=img/255
- img=cv.resize(img,self.dim)
- ### Convert the image to grayscale
- if self.n_channels==1:
- img=convert_to_grayscale(img)
- ### Apply other transformation whenever required
- if self.rotation_range!=None:
- img=random_rotation(img,self.rotation_range)
- if self.width_shift_range!=None:
- img=random_shift(img,wrg=self.width_shift_range,hrg=0)
- if self.height_shift_range!=None:
- img=random_shift(img,hrg=self.height_shift_range,wrg=0)
- if self.brightness_range!=None:
- img=random_brightness(img,brightness_range=self.brightness_range)
- if self.horizontal_flip==True:
- img=cv.flip(img, 1)
-
-
- X[i,]=img
- ### Store the labels
- y[i]=self.labels[img_path]
-
- return X,y
- def create_model(img_size):
- cnn_model = Sequential([
- # First block
- Conv2D(32, 3, activation='relu', padding='same', strides=2,
- input_shape=img_size),
- MaxPooling2D(pool_size=2, strides=2),
- # Second block
- Conv2D(64, 3, activation='relu', padding='same', strides=2),
- MaxPooling2D(pool_size=2, strides=2),
- # Flatenning
- Flatten(),
- # Fully connected layers
- Dense(128, activation='relu'),
- Dense(1, activation='sigmoid') # Only 1 output
- ])
-
- cnn_model.compile(
- optimizer=Adam(learning_rate=0.001), # Default lr
- loss='binary_crossentropy',
- metrics=['accuracy',tf.keras.metrics.Precision(name='precision'), tf.keras.metrics.Recall(name='recall')])
-
- return cnn_model
- def get_experiment_id(name):
- exp = mlflow.get_experiment_by_name(name)
- if exp is None:
- exp_id = mlflow.create_experiment(name)
- return exp_id
- return exp.experiment_id
- def train_model(experiment_name,train_data_gen,test_data_gen,input_image_dim,epochs=20):
- #mlflow.create_experiment(RUN_NAME)
- tf.keras.backend.clear_session()
- experiment_id=get_experiment_id(experiment_name)
- mlflow.tensorflow.autolog()
-
- with mlflow.start_run(experiment_id=experiment_id) as run:
- print("Run ID ",run.info.run_id)
- cnn_model=create_model(input_image_dim)
- # Display summary of model architecture
- cnn_model.summary()
- num_training_samples=train_data_gen.sample_size
- #num_validation_samples=validation_data_gen.sample_size
- mlflow.log_param("training_sample_size", num_training_samples)
- #mlflow.log_param("validation_sample_size",num_validation_samples)
- n_epochs = epochs
- early_stopping=EarlyStopping(monitor='loss', patience=4,restore_best_weights=True)
- history=cnn_model.fit(
- train_data_gen,
-
- epochs=n_epochs,
- verbose=1,callbacks=[early_stopping])
- y_pred_prob = cnn_model.predict(test_data_gen, verbose=1)
- y_pred = (y_pred_prob >= 0.5).reshape(-1,)
- y_true = list(test_data_gen.labels.values())
- test_metrics={'precision_score':precision_score(y_true,y_pred),"recall_score":recall_score(y_true,y_pred),"accuracy_score":accuracy_score(y_true,y_pred)}
- mlflow.log_metric("test_precision",test_metrics['precision_score'])
- mlflow.log_metric("test_recall",test_metrics['recall_score'])
- mlflow.log_metric("test_accuracy",test_metrics['accuracy_score'])
- mlflow.log_param("test_sample_size",test_data_gen.sample_size)
- mlflow.end_run()
- run_id=run.info.run_id
-
- return cnn_model,run_id,history,test_metrics
-
- def building_model(query_data,experiment_name,batch_size=16,n_channels=1,train_test_split=1,dim=(300,300),n_epochs=20):
- train_data_generator=ActiveLearningDataGenerator(query_data,data_directory=TRAIN_DATA_PATH,mode="train",batch_size=batch_size,n_channels=n_channels,train_test_split=train_test_split,dim=dim,shuffle=True)
- #validation_data_generator=ActiveLearningDataGenerator(query_data,data_directory=TRAIN_DATA_PATH,mode="validation",batch_size=batch_size,n_channels=n_channels,train_test_split=train_test_split,dim=dim,shuffle=True)
- img_size=dim+(n_channels,)
- print("Image Size",img_size)
-
- test_data_generator=DataGenerator(data_directory=TEST_DATA_PATH,mode="test",batch_size=1,n_channels=1,train_test_split=1,dim=(300,300),shuffle=False)
- cnn_model,run_id,history,test_metrics=train_model(experiment_name,train_data_generator,test_data_generator,img_size,epochs=n_epochs)
-
- return cnn_model,run_id,history,test_metrics
-
-
- ITERATION_COUNT = 0
- CLEANED_IMAGES_ARRAY=None
- def query_the_oracle(data,initial_pool_size=INITIAL_TRAINING_SAMPLES,query_size=NUM_SAMPLES_LABELLED_PER_ITERATION,folder_paths=None,query_strategy="random",classifier_model=None,image_dim=(300,300),n_channels=1,use_cleaned_array=False):
- '''
- This method will extract data points for the oracle or the human to label.
- '''
- global ITERATION_COUNT
- global CLEANED_IMAGES_ARRAY
-
- unlabelled_data=data[data['labelled']==False]
- unlabelled_images=unlabelled_data['file_name'].tolist()
-
- #labelled_data=data[data['labelled']==True]
-
- #num_samples_unlabelled=unlabelled_data.shape[0]
- if data[data['labelled']==True].shape[0]==0:
- ### If this is the first round of iteration , that is there is no labelled data - choose data points randomly to label by oracle
- ## Here, I am using the name of the image file to label, but we can also ask the user to label.
-
- print("In First Iteration - so default using Random Query Strategy")
- images_to_label=random_query_strategy(unlabelled_images,initial_pool_size)
- #images_to_label=random.sample(unlabelled_images,num_samples_to_label)
-
-
- else:
- if query_strategy=="random":
- print("Choosing for the Unlabelled data random samples")
- images_to_label=random_query_strategy(unlabelled_images,query_size)
-
- if query_strategy=="entropy":
- print("Choosing samples from the unlabelled data where the model is not confident about")
- if ITERATION_COUNT==1 and use_cleaned_array==False:
- ### Clean the Images and save it as a numpy array. We will also upload the Numpy array to Dagshub
- cleaned_images={}
- print("Cleaning The Images")
- for folder_path in folder_paths:
- cleaned_images.update(generate_clean_images(folder_path,image_dim,n_channels))
- print("Number of Images in the Cleaned Data ",len(cleaned_images))
- print('Done Cleaning')
- np.save('cleaned_images_numpy_array.npy',cleaned_images)
- print("Saved Cleaned Images Numpy Array")
- repo = Repo(DAGSHUB_USERNAME,DAGSHUB_REPO_NAME,branch="main",username=DAGSHUB_USERNAME,token=DAGSHUB_TOKEN)
- repo.upload(file="cleaned_images_numpy_array.npy",path="cleaned_images_numpy_array.npy",commit_message="Updating Cleaned Images Numpy Array",versioning="dvc")
- print("Loading the Cleaned Images Numpy Array")
- ## If it is the first iteration and we need to use the cleaned_array,load it from Dagshub
- if ITERATION_COUNT==1 and use_cleaned_array==True:
- ### Pull the file from the Repo and calculate the entropy
- fs=create_streaming_client()
- numpy_file_path=os.path.join(DAGSHUB_REPO_NAME,"cleaned_images_numpy_array.npy")
- fs.open(numpy_file_path)
- cleaned_images=np.load(numpy_file_path,allow_pickle=True)
- cleaned_images=cleaned_images[()]
- CLEANED_IMAGES_ARRAY=cleaned_images
-
- ### Run the Prediction on the data and choose the unlabelled samples to label
- images_to_label=entropy_query_strategy(unlabelled_images,query_size,classifier_model,cleaned_images=CLEANED_IMAGES_ARRAY)
- data.loc[data['file_name'].isin(images_to_label),'labelled']=True
-
- data['class_assigned']=data.apply(lambda row:label_image(row['file_name'],row['labelled']),axis=1)
-
- print(ITERATION_COUNT)
- ITERATION_COUNT=ITERATION_COUNT+1
-
- return data
- '''
- This data generator reads the data from a given path and generates batches of data.
- This is used to generate training and validation data for Active Learning
- '''
- class ActiveLearningDataGenerator(keras.utils.Sequence):
- 'Generates data for Keras'
-
- def __init__(self,dataframe,data_directory,mode='train', image_cls={'ok':0,'defect':1},
- batch_size=32, dim=(150, 150), n_channels=3, shuffle=True,train_test_split=0.8,
- rotation_range=None, width_shift_range=None, height_shift_range=None,horizontal_flip=False,brightness_range=None):
- """
- Initialise the data generator
- """
- self.dim = dim
- self.batch_size = batch_size
- self.labels = {}
- self.list_IDs = []
- self.rotation_range=rotation_range
- self.horizontal_flip=horizontal_flip
- self.brightness_range=brightness_range
- self.height_shift_range=height_shift_range
- self.width_shift_range=width_shift_range
- self.fs=create_streaming_client()
-
-
-
- # glob through directory of each class
-
-
- ### Filter the dataframe only for labelled data
-
- dataframe=dataframe[dataframe['labelled']==True]
- print("Number of Labelled Data for This Iteration is ",dataframe.shape[0])
- label_class=[key for key,val in image_cls.items()]
-
- for i, class_name in enumerate(label_class):
- ### Get the number of images in both classes and split the data into training and validation
-
-
-
- num_images=dataframe[dataframe['class_assigned']==class_name].shape[0]
- print("number of images in "+class_name+" is "+str(num_images))
-
- brk_point = int(num_images*train_test_split) #Divide the data into 80:20 - training and validation set
-
- file_paths=dataframe.loc[dataframe['class_assigned']==class_name,'file_name'].tolist()
-
-
-
- #file_paths=os.listdir(os.path.join(data_directory,class_name+"/"))
- if class_name=="ok":
- cls_name="ok"
- if class_name=="defect":
- cls_name="def"
- folder_path=os.path.join(data_directory,cls_name+"_front/")
- print(folder_path)
- if mode == 'train':
- file_paths = file_paths[:brk_point]
- else:
- file_paths = file_paths[brk_point:]
-
- file_paths=[os.path.join(folder_path,file_path) for file_path in file_paths]
- self.list_IDs += file_paths
- self.labels.update({p:image_cls[class_name] for p in file_paths})
- print("Number of Files For "+mode+"for class "+class_name+" is "+str(len(file_paths)))
-
- self.n_channels = n_channels
- self.n_classes = len(label_class)
- self.shuffle = shuffle
- self.sample_size=len(self.list_IDs)
- self.on_epoch_end()
-
- self.on_epoch_end()
- def __len__(self):
- 'Denotes the number of batches per epoch'
- return int(np.floor(len(self.list_IDs) / self.batch_size))
- def __getitem__(self, index):
- 'Generate one batch of data'
- # Generate indexes of the batch
- indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
- # Find list of IDs
- #list_IDs_temp = [self.list_IDs[k] for k in indexes]
- # Generate data
- X, y = self.__data_generation(indexes)
- return X, y
- def on_epoch_end(self):
- 'Updates indexes after each epoch. If shuffle is true, this will shuffle the dataset'
- print("In On_EPOCH_END")
- self.indexes = np.arange(len(self.list_IDs))
- if self.shuffle == True:
- np.random.shuffle(self.indexes)
-
-
- def __data_generation(self, indexes):
-
- 'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
- # Initialization
- X = np.empty((self.batch_size, *self.dim, self.n_channels))
- y = np.empty((self.batch_size), dtype=int)
-
- ### Generate data based on the indexes.
- for i,idx in enumerate(indexes):
- #image_id=self.images[idx]
- img_path=self.list_IDs[idx]
-
- #print(img_path)
- ### Read the Images using Streaming Client
- img=read_images_streaming(self.fs,img_path)
-
- img=img/255
- img=cv.resize(img,self.dim)
- ### Convert the image to grayscale
- if self.n_channels==1:
- img=convert_to_grayscale(img)
- ### Apply other transformation whenever required
- if self.rotation_range!=None:
- img=random_rotation(img,self.rotation_range)
- if self.width_shift_range!=None:
- img=random_shift(img,wrg=self.width_shift_range,hrg=0)
- if self.height_shift_range!=None:
- img=random_shift(img,hrg=self.height_shift_range,wrg=0)
- if self.brightness_range!=None:
- img=random_brightness(img,brightness_range=self.brightness_range)
- if self.horizontal_flip==True:
- img=cv.flip(img, 1)
-
-
- X[i,]=img
- ### Store the labels
- y[i]=self.labels[img_path]
- #print("Data Shape Printing")
- #print(X.shape)
- #print(y.shape)
-
- return X,y
-
- def set_end_flag(num_labelled_samples,threshold_sampling):
- if num_labelled_samples>=threshold_sampling:
- return False
- return True
- def get_num_unlabelled(train_data):
- return train_data[train_data['labelled']==False].shape[0]
- def get_num_labelled(train_data):
- return train_data[train_data['labelled']==True].shape[0]
- def clean_image(fs,file_path,image_dim,n_channels):
- img=read_images_streaming(fs,file_path)
- img=img/255
- img=cv.resize(img,image_dim)
- if n_channels==1:
- img=convert_to_grayscale(img)
- return img
- def get_iteration_labelled(is_labelled,current_iter_count,iteration_count):
- if is_labelled==False:
- return -1
- else:
- if current_iter_count!=-1:
- return current_iter_count
- else:
- return iteration_count
- #### We will use MLFLOW To track our experiments - and the results we will store it on Dagshub
- if __name__ == "__main__":
- '''
- This should take the following parameters as arguments
- 1. num_epochs -- done, optional. dafult is 20
- 2. initial_pool_size -- done, optional
- 3. query_size -- done,optional
- 4. thresholding samples size. done, optiona. default value is NUM_SAMPLES_LABELLED_PER_ITERATION
- 5. experiment_name -- done,mandatory
- 6. query_strategy -- done,optional.default is "random". Takes value either random or entropy
- 7. batch_size -- done, optional. default is 16
- 8. image_dim -- done, optional. default is (300,300)
- 9. num_channels -- done, optional default is 1
-
- '''
- parser = argparse.ArgumentParser()
- parser.add_argument('--initial_query_size', type=int, required=False)
- parser.add_argument('--pool_size', type=int, required=False)
- parser.add_argument('--query_size',type=int,required=False)
- parser.add_argument('--query_strategy',type=str,required=False,choices=['random', 'entropy'])
- parser.add_argument('--experiment_name',type=str,required=True)
- parser.add_argument('--n_epochs',type=int,required=False)
- parser.add_argument('--batch_size',type=int,required=False)
- parser.add_argument('--image_dim',type=int, nargs=2,required=False)
- parser.add_argument('--n_channels',type=int,required=False)
- parser.add_argument("--threshold_sampling",type=int,required=False)
- parser.add_argument("--use_cleaned_array",type=int,required=False)
- args = parser.parse_args()
- if args.initial_query_size:
- INITIAL_TRAINING_SAMPLES=args.initial_query_size
- if args.query_size:
- NUM_SAMPLES_LABELLED_PER_ITERATION=args.query_size
- if args.query_strategy:
- query_strategy=args.query_strategy
- else:
- query_strategy="random"
- if args.pool_size:
- POOL_SIZE=args.pool_size
-
- if args.use_cleaned_array:
- if args.use_cleaned_array==0:
- CLEANED_ARRAY=False
- else:
- CLEANED_ARRAY=True
- else:
- CLEANED_ARRAY=False
- if args.n_epochs:
- n_epochs=args.n_epochs
- else:
- n_epochs=20
- if args.batch_size:
- batch_size=args.batch_size
- else:
- batch_size=16
- if args.image_dim:
- dim=tuple(args.image_dim)
- else:
- dim=(300,300)
-
- if args.n_channels:
- n_channels=args.n_channels
- else:
- n_channels=1
- if args.threshold_sampling:
- threshold_sampling=args.threshold_sampling
- else:
- threshold_sampling=MAX_SAMPLES_LABELLED
-
- experiment_name=args.experiment_name
-
- ### Create the TRain DataFrame with the list of images, initially all of them are unlaballed
- TRAIN_OK_DATA_PATH=os.path.join(TRAIN_DATA_PATH,OK_DATA_FOLDER)
- TRAIN_DEFECT_DATA_PATH=os.path.join(TRAIN_DATA_PATH,DEFECT_DATA_FOLDER)
- if query_strategy=="entropy":
- FOLDER_PATHS=[TRAIN_OK_DATA_PATH,TRAIN_DEFECT_DATA_PATH]
- else:
- FOLDER_PATHS=None
- train_data=create_train_data_frame(TRAIN_OK_DATA_PATH,TRAIN_DEFECT_DATA_PATH)
-
- train_data['iteration_labelled']=-1
- ### We will run Active Learning Loop till there is very less change in the test accuracy or till all the training samples have been labelled
- current_accuracy=0
-
- num_labelled_samples=get_num_labelled(train_data)
- print(num_labelled_samples,threshold_sampling)
- COUNT_ITER=0
- query_data=train_data.copy()
- #experiment_name="active-learning-random"
- count=0
- classifier_model=None
- while set_end_flag(num_labelled_samples,threshold_sampling):
- ## Query the oracle and get the Data Samples Labelled
- if classifier_model!=None:
- print("Classifier Model Not None")
- else:
- print("Classifier Model None")
- query_data=query_the_oracle(query_data,initial_pool_size=INITIAL_TRAINING_SAMPLES,query_size=NUM_SAMPLES_LABELLED_PER_ITERATION,folder_paths=FOLDER_PATHS,query_strategy=query_strategy,classifier_model=classifier_model,image_dim=dim,n_channels=n_channels,use_cleaned_array=CLEANED_ARRAY)
-
- ### For the labelled data - set the iteration_number to count
- query_data['iteration_labelled']=query_data.apply(lambda row:get_iteration_labelled(row['labelled'],row['iteration_labelled'],count),axis=1)
- query_data.to_excel("Query_Data_Labelling_"+query_strategy+".xlsx",index=False)
-
- #repo = Repo(DAGSHUB_USERNAME,DAGSHUB_REPO_NAME,branch="main",username=DAGSHUB_USERNAME,token=DAGSHUB_TOKEN)
- #repo.upload(file="Query_Data_Labelling_"+query_strategy+".xlsx",path="query_data_label_"+query_strategy+"/Query_Data_Labelling_"+query_strategy+".xlsx",commit_message="Updating Query Data for "+query_strategy+" after iteration "+str(count))
- print("Number of Samples Labelled ",get_num_labelled(query_data))
- print("Number of Samples Unlabelled ",get_num_unlabelled(query_data))
- cnn_model,run_id,history,test_metrics=building_model(query_data,experiment_name=experiment_name,n_epochs=n_epochs,batch_size=batch_size,dim=dim,n_channels=n_channels)
- prev_accuracy=current_accuracy
- current_accuracy=test_metrics['accuracy_score']
- print("Prev Accuracy ",prev_accuracy)
- print("Test Accuracy For Iteration "+str(COUNT_ITER)+" is "+str(current_accuracy))
- #print("Train Accuracy For Iteration "+str(COUNT_ITER)+" is "+str(history.history['acc']))
- print("Change is Test Accuracy ",str(current_accuracy-prev_accuracy))
- num_unlabelled_samples=get_num_unlabelled(query_data)
- num_labelled_samples=get_num_labelled(query_data)
- classifier_model=cnn_model
- count=count+1
-
|