Skip to content

Commit

Permalink
Critical Case Trained & Created'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoonseo Kim committed Dec 5, 2020
1 parent 09193f0 commit bf26de6
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 100 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ML/data
eval_data
eval_data
created
Binary file added Critical Cases/C1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/C2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/C3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/C4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/C5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/C6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/O1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/O2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/O3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/O4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/O5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Critical Cases/O6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 30 additions & 31 deletions GA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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')

Expand All @@ -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]

Expand Down Expand Up @@ -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]
Expand All @@ -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부모를 픽함
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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)
3 changes: 2 additions & 1 deletion ML/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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' :
Expand Down
Binary file added ML/trained_model/medium_82.pth
Binary file not shown.
Binary file modified ML/utils/__pycache__/dataloader.cpython-37.pyc
Binary file not shown.
10 changes: 5 additions & 5 deletions ML/utils/dataloader.py
Original file line number Diff line number Diff line change
@@ -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(),
]))

Expand All @@ -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(),
]))

Expand Down
File renamed without changes
Binary file added base_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions create_image.py
Original file line number Diff line number Diff line change
@@ -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)


62 changes: 0 additions & 62 deletions deter.py

This file was deleted.

Binary file removed test.png
Binary file not shown.
Binary file modified utils/__pycache__/dataloader.cpython-37.pyc
Binary file not shown.

0 comments on commit bf26de6

Please sign in to comment.