diff --git a/.gitignore b/.gitignore index 60f8044..0cc8029 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ ML/data -eval_data \ No newline at end of file +eval_data +created \ No newline at end of file diff --git a/Critical Cases/C1.png b/Critical Cases/C1.png new file mode 100644 index 0000000..75c069e Binary files /dev/null and b/Critical Cases/C1.png differ diff --git a/Critical Cases/C2.png b/Critical Cases/C2.png new file mode 100644 index 0000000..733559b Binary files /dev/null and b/Critical Cases/C2.png differ diff --git a/Critical Cases/C3.png b/Critical Cases/C3.png new file mode 100644 index 0000000..9e01e92 Binary files /dev/null and b/Critical Cases/C3.png differ diff --git a/Critical Cases/C4.png b/Critical Cases/C4.png new file mode 100644 index 0000000..e99896a Binary files /dev/null and b/Critical Cases/C4.png differ diff --git a/Critical Cases/C5.png b/Critical Cases/C5.png new file mode 100644 index 0000000..ce824bf Binary files /dev/null and b/Critical Cases/C5.png differ diff --git a/Critical Cases/C6.png b/Critical Cases/C6.png new file mode 100644 index 0000000..22b698c Binary files /dev/null and b/Critical Cases/C6.png differ diff --git a/Critical Cases/O1.png b/Critical Cases/O1.png new file mode 100644 index 0000000..47567ee Binary files /dev/null and b/Critical Cases/O1.png differ diff --git a/Critical Cases/O2.png b/Critical Cases/O2.png new file mode 100644 index 0000000..3b81e50 Binary files /dev/null and b/Critical Cases/O2.png differ diff --git a/Critical Cases/O3.png b/Critical Cases/O3.png new file mode 100644 index 0000000..326ee9b Binary files /dev/null and b/Critical Cases/O3.png differ diff --git a/Critical Cases/O4.png b/Critical Cases/O4.png new file mode 100644 index 0000000..b58b7dd Binary files /dev/null and b/Critical Cases/O4.png differ diff --git a/Critical Cases/O5.png b/Critical Cases/O5.png new file mode 100644 index 0000000..1c7f22a Binary files /dev/null and b/Critical Cases/O5.png differ diff --git a/Critical Cases/O6.png b/Critical Cases/O6.png new file mode 100644 index 0000000..4acffcd Binary files /dev/null and b/Critical Cases/O6.png differ diff --git a/GA.py b/GA.py index 23cac2f..4a9f317 100644 --- a/GA.py +++ b/GA.py @@ -44,8 +44,8 @@ def prepare_model(): iaa.SigmoidContrast(gain=10, cutoff=0.3), iaa.LogContrast(0.7), iaa.LogContrast(1.3), - iaa.Sharpen(alpha=0.5, lightness=0.9), - iaa.Sharpen(alpha=0.5, lightness=1.2), + iaa.Sharpen(alpha=0.2, lightness=0.9), + iaa.Sharpen(alpha=0.2, lightness=1.2), iaa.Fliplr(1), iaa.Flipud(1), iaa.Rotate(15), @@ -60,24 +60,17 @@ def prepare_model(): iaa.ScaleY(1.3), ] -def custom_imshow(original_imgList, imgList, predicted): +def custom_imshow(imgList, labels): fig = plt.figure() rows = 2 cols = 5 - - for i in range(5): - img = original_imgList[i] - temp = fig.add_subplot(rows, cols, i+1) - temp.set_title(classes[predicted[i]]) - temp.imshow(np.transpose(img, (1, 2, 0))) - temp.axis('off') - for i in range(5): + for i in range(10): img = imgList[i] - temp = fig.add_subplot(rows, cols, i+6) - temp.set_title(classes[predicted[i]]) + temp = fig.add_subplot(rows, cols, i+1) + temp.set_title(classes[labels[i]]) temp.imshow(np.transpose(img, (1, 2, 0))) temp.axis('off') @@ -102,15 +95,13 @@ def aug_GA(label, augList, popN, genN, rate, target_score, model): gen = gen0 gen_num = 0 - status = True finAug = [] finFit = [] - while status and gen_num < 40: + while gen_num < 40: new_gen = GA(label, augList, gen, genN, rate, model) for idx, son in enumerate(new_gen): if son[1] > target_score: - status = False finAug = son[0] finFit = son[1] @@ -169,9 +160,6 @@ def label_fit(labelIdx, augList, model): resList = torch.softmax(outputs, dim=-1).tolist() _, predicted = torch.max(outputs.data, 1) - # print(str(augList[0].__class__.__name__) + '\n' + str(augList[1].__class__.__name__) + '\n' + str(augList[2].__class__.__name__) + '\n' + str(augList[3].__class__.__name__) + '\n') - # custom_imshow(inputs, aug_im, predicted) - for i in range(10): first = sorted(resList[i])[4] @@ -185,12 +173,25 @@ def label_fit(labelIdx, augList, model): f = (1-resList[i][labelIdx] + 0.5*(1-first+second) + var) * np.exp(first-resList[i][labelIdx]) fitnessList.append(f) # [Tensor] - + # calculate the total fitness as average of 10 fitnesses fitnessTotal = np.mean(fitnessList) - print('Fintess : {}'.format(fitnessTotal)) - return fitnessTotal + """ + if fitnessTotal > 2.8 : + + print(augList[0]) + print('\n') + print(augList[1]) + print('\n') + print(augList[2]) + print('\n') + custom_imshow(inputs, labels) + custom_imshow(aug_im, predicted) + + print(fitnessTotal) + """ + return fitnessTotal ######## # gen list를 받아서 룰렛-휠 방식에 따라 2개의 스코어가 높은 aug부모를 픽함 @@ -228,7 +229,7 @@ def mutate(augList, C ,rate): def GA(label, augList, gen, genN, rate, model): new_gen = [] gen_fitness = 0 - + for i in range(genN): A, B = roulette(gen) C = crossover(augList, A, B) @@ -242,7 +243,10 @@ def GA(label, augList, gen, genN, rate, model): for gene in new_gen: gen_fitness += gene[1] - fits.append(gen_fitness/len(new_gen)) + average_fitness = gen_fitness/len(new_gen) + fits.append(average_fitness) + + print('Avearge Fitness : {}'.format(average_fitness)) return new_gen @@ -252,16 +256,11 @@ def GA(label, augList, gen, genN, rate, model): fits = [] - fin_aug, fin_fit = aug_GA(0, augList, 50, 20, 0.05, 3.5, model) + fin_aug, fin_fit = aug_GA(0, augList, 50, 20, 0.05, 3.0, model) smoother = gaussian_filter1d(fits, sigma=1) plt.plot(smoother) - plt.savefig('test_1.png') + plt.savefig('after_1.png') plt.title('Fitness Graph') plt.show() - - print(fin_aug[0]) - print(fin_aug[1]) - print(fin_aug[2]) - print(fin_fit) diff --git a/ML/main.py b/ML/main.py index 953c98d..674d7d0 100644 --- a/ML/main.py +++ b/ML/main.py @@ -113,7 +113,8 @@ def parsing(correct, total, class_correct, class_total): def main(): # Is GPU Available? - device = torch.device("cuda: 0" if torch.cuda.is_available() else "cpu") + #device = torch.device("cuda: 0" if torch.cuda.is_available() else "cpu") + device = torch.device('cpu') print("Current Using device : {}".format(device)) if args.model == 'small' : diff --git a/ML/trained_model/medium_82.pth b/ML/trained_model/medium_82.pth new file mode 100644 index 0000000..662d2c1 Binary files /dev/null and b/ML/trained_model/medium_82.pth differ diff --git a/ML/utils/__pycache__/dataloader.cpython-37.pyc b/ML/utils/__pycache__/dataloader.cpython-37.pyc index 06fabe4..a6148e5 100644 Binary files a/ML/utils/__pycache__/dataloader.cpython-37.pyc and b/ML/utils/__pycache__/dataloader.cpython-37.pyc differ diff --git a/ML/utils/dataloader.py b/ML/utils/dataloader.py index 75cdeb5..6e62b6b 100644 --- a/ML/utils/dataloader.py +++ b/ML/utils/dataloader.py @@ -1,16 +1,16 @@ import torch import torchvision from torchvision import transforms -from utils.randaugment import RandAugment +# from utils.randaugment import RandAugment import matplotlib.pyplot as plt import numpy as np -train_dataset = torchvision.datasets.ImageFolder(root='./data/train', +train_dataset = torchvision.datasets.ImageFolder(root='data/train', transform=transforms.Compose([ transforms.Resize((128,128)), # transforms.RandomHorizontalFlip(), # transforms.RandomVerticalFlip(), - # RandAugment(n=2,m=8), + # RandAugment(n=1,m=4), transforms.ToTensor(), ])) @@ -19,12 +19,12 @@ shuffle=True ) -val_dataset = torchvision.datasets.ImageFolder(root='./data/eval', +val_dataset = torchvision.datasets.ImageFolder(root='data/eval', transform=transforms.Compose([ transforms.Resize((128,128)), # transforms.RandomHorizontalFlip(), # transforms.RandomVerticalFlip(), - # RandAugment(n=2,m=8), + # RandAugment(n=1,m=4), transforms.ToTensor(), ])) diff --git a/test_1.png b/base_1.png similarity index 100% rename from test_1.png rename to base_1.png diff --git a/base_2.png b/base_2.png new file mode 100644 index 0000000..02136d4 Binary files /dev/null and b/base_2.png differ diff --git a/create_image.py b/create_image.py new file mode 100644 index 0000000..e263ded --- /dev/null +++ b/create_image.py @@ -0,0 +1,77 @@ + +import numpy as np +import imgaug.augmenters as iaa +from ML.utils.dataloader import train_loader +import cv2 +import random + +import matplotlib.pyplot as plt + +critical_1 = [ + iaa.SaltAndPepper(0.05), + iaa.Sharpen(alpha=0.2, lightness=0.9), + iaa.ScaleX(0.7) +] + +critical_2 = [ + iaa.SaltAndPepper(0.05), + iaa.Sharpen(alpha=0.2, lightness=0.9), + iaa.Flipud(1) +] + +critical_3 =[ + iaa.SaltAndPepper(0.05), + iaa.Sharpen(alpha=0.2, lightness=0.9), + iaa.ShearY(10) +] + +critical_4 =[ + iaa.SaltAndPepper(0.05), + iaa.Sharpen(alpha=0.2, lightness=0.9), + iaa.Add(20) +] + +critical_5 =[ + iaa.SaltAndPepper(0.05), + iaa.Sharpen(alpha=0.2, lightness=0.9), + iaa.Multiply(1.3) +] + +critical_6 =[ + iaa.SaltAndPepper(0.05), + iaa.Sharpen(alpha=0.2, lightness=0.9), + iaa.LogContrast(1.3) +] + +critical_cases = [critical_1, critical_2, critical_3, critical_4, critical_5, critical_6] + +# Our Dataset Classes +classes = ('airplane', 'cat', 'dog', 'motorbike', 'person') + +def ImgTransform(images, TransformList): + + seq = iaa.Sequential([ + TransformList[0], + TransformList[1], + TransformList[2], + ]) + + I = cv2.normalize(images.permute(0,2,3,1).numpy(), None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) # 10, 128, 128, 3 + result = seq(images=I) + return np.array(result, dtype='float32') / 255 # batchSize , 128, 128, 3 + + +for idx, data in enumerate(train_loader): + + inputs, labels = data[0], data[1] # Batchsize : 30 + rand_aug = random.randint(0,5) + + aug_im = ImgTransform(inputs, critical_cases[rand_aug]) + for img_idx, img in enumerate(aug_im) : + + # plt.imshow(img) + # plt.show() + + plt.imsave('created/{}/{}_{}.png'.format(classes[labels[img_idx]], idx, img_idx), img) + + \ No newline at end of file diff --git a/deter.py b/deter.py deleted file mode 100644 index 4477297..0000000 --- a/deter.py +++ /dev/null @@ -1,62 +0,0 @@ -import imgaug.augmenters as iaa -import cv2 -import matplotlib.pyplot as plt -import numpy as np -from PIL import Image - -iaa.Add(40) -iaa.Add(-40) -iaa.Multiply(0.7) -iaa.Multiply(1.5) -iaa.Cutout(fill_mode="constant", cval=(0, 255), fill_per_channel=1) -iaa.SaltAndPepper(0.05) -iaa.GaussianBlur(1.5) -iaa.MotionBlur(k=15, angle=60, direction=1) -iaa.MotionBlur(k=5, angle=60, direction=-1) -iaa.Grayscale(0.5) -iaa.SigmoidContrast(gain=10, cutoff=0.3) -iaa.LogContrast(0.7) -iaa.LogContrast(1.3) -iaa.Sharpen(alpha=0.5, lightness=0.8) -iaa.Sharpen(alpha=0.5, lightness=1.2) -iaa.Fliplr(1) -iaa.Flipud(1) -iaa.Rotate(-60) -iaa.Rotate(60) -iaa.ShearX(-20) -iaa.ShearX(20) -iaa.ShearY(-20) -iaa.ShearY(20) -iaa.ScaleX(0.5) -iaa.ScaleX(1.5) -iaa.ScaleY(0.5) -iaa.ScaleY(1.5) - - -augList = [iaa.Add((-40, 40)), - iaa.Multiply((0.5, 1.5)), - iaa.Cutout(fill_mode="constant", cval=(0, 255), fill_per_channel=1), - iaa.SaltAndPepper(0.1), - iaa.GaussianBlur(sigma=(0.0, 3.0)), - iaa.MotionBlur(k=15, angle=[-45, 45]), - iaa.Grayscale(alpha=(0.0, 1.0)), - iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)), - iaa.LogContrast(gain=(0.6, 1.4)), - iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)), - iaa.Fliplr(1), - iaa.Flipud(1), - iaa.Rotate((-45, 45)), - iaa.ShearX((-20, 20)), - iaa.ShearX((-20, 20)), - iaa.ScaleX((0.5, 1.5)), - iaa.ScaleY((0.5, 1.5)), - ] - -img = np.array(Image.open('eval_data/aug/cat/cat_0307.jpg')) -aug = iaa.Multiply(0.7) -add_dark = iaa.Add(-40) - -plt.imshow(iaa.Sharpen(alpha=0, lightness=0.8).augment_image(img)) - - -plt.show() \ No newline at end of file diff --git a/test.png b/test.png deleted file mode 100644 index 090c8a5..0000000 Binary files a/test.png and /dev/null differ diff --git a/utils/__pycache__/dataloader.cpython-37.pyc b/utils/__pycache__/dataloader.cpython-37.pyc index 81c6c39..f8cfe93 100644 Binary files a/utils/__pycache__/dataloader.cpython-37.pyc and b/utils/__pycache__/dataloader.cpython-37.pyc differ