Python 입력 값으로 출력 하기.


# 입력을 하나만 받을 시

age = 27 

print("나의 나이 : {0}".format(age))

print("나의 나이 : %d"%age)



# 입력을 두 개 이상 받을 시

name = '문주영'

age = 27


print("나의 이름 : {0} 나의 나이 : {1}".format(name,age))

print("나의 이름 : %s 나의 나이 : %d"%(name,age))


#파일 저장 시 for 문에 따른 네이밍

for i in range(3):

    print("파일번호_{0}".format(i))

    

for i in range(3):

    print("파일번호_%d"%(i))



MNIST DATA

  • Imbalance data set을 만들기 위한 코드.
  • 클래스 라벨과 갯수를 입력으로 받는 함수.
  • 랜덤으로 데이터가 추출되며, 트레이닝 데이터셋을 만들시 리스트에 계속 추가 가능.


from tensorflow.examples.tutorials.mnist import input_data

import random

import numpy as np

import pandas as pd


# 데이터 추출

def Creat_Imbalance_Mnist_data(class_label, data_count,mnist_x,mnist_y):

    # 데이터 생성 개수 체크.

    count = 0

    # 생성 데이터 저장할 리스트.

    li_x_data = []

    li_y_data = []


    while count < data_count:

        random_num = random.randint(1,49999)

        

        if(mnist_y[random_num] == class_label):

            li_x_data.append(mnist_x[random_num])

            li_y_data.append(mnist_y[random_num])

            count+=1

            

    return li_x_data, li_y_data


# one-hot-encoding

def one_hot_encoding(label):

    cls = set(label)

    class_dict = {c: np.identity(len(cls))[i, :] for i, c in enumerate(cls)}

    one_hot = np.array(list(map(class_dict.get, label)))

    

    return one_hot


# Mnist 데이터 불러오기.

mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)


origin_data_x,origin_data_y =  mnist.train.next_batch(50000)


# 라벨 순서 저장.

label = [0,1,2,3,4,5,6,7,8,9]

# 데이터 갯수.

count = [10,20,30,40,50,60,70,80,90,300]


# 함수 데이터를 받아올 리스트

train_data_x = []

train_data_y = []


# 데이터 추출

for i in range(10):

    data_x, data_y = Creat_Imbalance_Mnist_data(label[i],count[i],origin_data_x,origin_data_y)

    

    train_data_x.append(data_x)

    train_data_y.append(data_y)


# x 차원 낮추기.

train_x = [a for i in train_data_x for a in i]

# one - hot _encoder

one_hot = [a for i in train_data_y for a in i]

train_y_one_hot = one_hot_encoding(one_hot)


print(len(train_x))

print(len(train_y_one_hot))


스케쥴링 병렬 처리

  • 웨이트 타임을 비교해, 우선 큐 지정 후 작업 할당.


import numpy as np

import queue


# Interval Time , Service time 변수  

lamb=float(5) 

mu = float(3) 


# 큐생성

queue1 = queue.Queue()

queue2 = queue.Queue()


# 처음 작업이 들어오는 경우.

Fisrt_Arrival = np.random.exponential(lamb)

First_Service = np.random.exponential(mu) 


# 처음 작업 출력.

print("First_Arrival : ", Fisrt_Arrival)

print("First_Service : ", First_Service)

print("---------------------------------------------------")


# 변수 통일을 위해 저장.

Next_Arrival = Fisrt_Arrival

Next_Service = First_Service


# 카운트 만큼 실행.

count = 0

wait = 0

Finish_time1 = 0

Finish_time2 = 0

while count<10:

    count+=1

    

    # queue 1인지 2인지 알려 줄 스위치. queue가 1이면 0, queue가 1이면 1

    sw = 0

    Now_queue=0    

    

    if (Finish_time1-Next_Arrival) <= (Finish_time2-Next_Arrival):

        queue1.put(Next_Arrival+Next_Service+wait)

        Now_queue = queue1

        sw = 0

    else:

        queue2.put(Next_Arrival+Next_Service+wait)

        Now_queue = queue2

        sw = 1

    

    # 도착 시간 갱신.

    while Next_Arrival < Next_Service:

        Next_Arrival += np.random.exponential(lamb)

        

    Finish_time = Now_queue.get()



    # 작업 종료 시간 가져오기.


    if sw == 0 :

        Finish_time1 = Finish_time

    else:

        Finish_time2 = Finish_time


    if (Finish_time1-Next_Arrival) <= (Finish_time2-Next_Arrival):

        if (Finish_time1 - Next_Arrival) > 0 :

            wait = Finish_time1 - Next_Arrival

        else:

            wait = 0

    else :

        if (Finish_time2 - Next_Arrival) > 0 :

            wait = Finish_time2 - Next_Arrival

        else:

            wait = 0   

    

    print("executed queue : %d" % (sw+1))

    print("Finsh1 : ", Finish_time1)

    print("Finsh2 : ", Finish_time2)

    # 서비스 시간 갱신.

    if Now_queue.empty():

        Next_Service = Next_Arrival + np.random.exponential(mu)

    else:

        Next_Service = Next_Service + np.random.exponential(mu)

    

    # 웨이트 시간 출력 및 시간..

    print("Next_Arrival : ", Next_Arrival)

    print("Next_Service : ", Next_Service)

    # 웨이트 시간 없을때의 예외처리.

    if(wait < 0):

        print("Wait Time : 0")

        wait = 0

    else:

        print("Wait Time : ", wait)

    

    print("---------------------------------------------------")


이미지의 바운딩 박스 영역으로 패치 만들기.

  • 바운딩 박스 좌표와 이미지를 입력으로 받아, 패치 사이즈를 지정해 패치를 만듬.
  • 새로운 좌표에서의 바운딩 좌표가 그려져 이미지 생성.
  • 가로 세로 지정가능.
  • 잘못된 바운딩 박스 좌표 입력시 예외 처리 기능.


import numpy as np

import pandas as pd

import cv2

import random

import os


def make_patch():

    # csv 파일 경로.

    csv_path_dir = r"C:\Users\User\연구\sa\csv_file"

    csv_list = os.listdir(csv_path_dir)

    # img 파일 경로.

    img_path_dir = r"C:\Users\User\연구\sa\img_file"

    img_list = os.listdir(img_path_dir)

    # csv파일에 있는 오브젝트 개수

    object_num = 0

    # csv 파일 만큼 불러오기.

    for csv_name in csv_list:

        file_name = csv_name.split('.')

        origin_csv = pd.read_csv('./csv_file/{}.csv'.format(file_name[0]))

        img_name = img_list[object_num]

        counting = 0

        object_num +=1

        # csv 파일 values 값 돌기.

        for origin_data in origin_csv.values: 

            # 이미지 파일 가져오기

            img = cv2.imread('./img_file/{}'.format(img_name))

            print(origin_data)            

            # 기존 이미지 좌표

            origin_x_min = 0

            origin_x_max = origin_data[1]

            origin_y_min = 0

            origin_y_max = origin_data[0]


            # 바운딩 박스 이미지 좌표.

            object_x_min = origin_data[2]

            object_x_max = origin_data[3]

            object_y_min = origin_data[4]

            object_y_max = origin_data[5]

            object_width = object_x_max - object_x_min

            object_height = object_y_max - object_y_min

            object_x_center = int(object_x_min + object_x_max )/2

            object_y_center = int(object_y_min + object_y_max )/2


            # csv 저장할 데이터 리스트

            bounding_box = []

            # 패치 개수 조정.

            patch_num = 0

            

            while(True):

                # 패치 개수에 따라서 종료.

                if (patch_num == 1):  break

                # patch 좌표 - (센터에서 +150 -150) 300 x 300 사이즈

                patch_x_min = int(object_x_center - 150)

                patch_x_max = int(object_x_center + 150)

                patch_y_min = int(object_y_center - 150)

                patch_y_max = int(object_y_center + 150)

                patch_width = int(patch_x_max - patch_x_min)

                patch_height = int(patch_y_max - patch_y_min)


                # object의 width와 height 더 클시 100씩 추가.

                while(object_width > patch_width):

                    patch_x_min = patch_x_min - 50

                    patch_x_max = patch_x_max + 50

                    patch_width = patch_x_max - patch_x_min

                    print(object_width,patch_width)


                # object의 width와 height 더 클시 100씩 추가.

                while(object_height > patch_height):

                    patch_y_min = patch_y_min - 50

                    patch_y_max = patch_y_max + 50

                    patch_height = patch_y_max - patch_y_min

                    print(object_width,object_height)

                    

                # 사각방향에 대한 예외처리. 패치 좌표가 원본 이미지보다 클시.

                if(patch_x_min < origin_x_min):

                    patch_x_min -= patch_x_min

                if(origin_x_max < patch_x_max):

                    val = patch_x_max - origin_x_max

                    patch_x_max -= val

                if(patch_y_min < origin_y_min):

                    patch_y_min -= patch_y_min

                if(origin_y_max < patch_y_max):

                    val = patch_y_max - origin_y_max

                    patch_y_max -= val

                # 갱신된 오브젝트 좌표.

                new_x_min = object_x_min - patch_x_min 

                new_x_max = object_x_max - patch_x_min 

                new_y_min = object_y_min - patch_y_min 

                new_y_max = object_y_max - patch_y_min 

                #리스트 추가.

                bounding_box.append([patch_width, patch_height,new_x_min,new_x_max,new_y_min,new_y_max,origin_csv['category'][0]])

                print(counting+1,img_name ,patch_x_min, patch_x_max,patch_y_min,patch_y_max,object_x_min,object_x_max

                      ,object_y_min,object_y_max,new_x_min,new_x_max,new_y_min,new_y_max,patch_height,object_height)

                #패치 사이즈로 이미지 자르고 저장.

                img_trim = img[patch_y_min:patch_y_max, patch_x_min:patch_x_max]

                cv2.rectangle(img_trim, (new_x_min, new_y_min), (new_x_max, new_y_max), (0,255,0), 1)

                print(object_num)

                cv2.imwrite('./patch_img/{}_{}.png'.format(file_name[0],counting+1), img_trim)                                        

                # 패치 개수 카운팅.

                patch_num += 1

                counting += 1


'Study > Deep Learning' 카테고리의 다른 글

Backpropagation  (0) 2018.12.27
MNIST Data 랜덤 추출.  (3) 2018.11.15
Visiualizing t-SNE(Stochastic Neighbor Embedding)  (0) 2018.09.03
Softmax fuction 소프트맥스 함수  (0) 2018.08.08
Gradient Descent Optimiztion  (0) 2018.07.18

NLTK를 이용한 Frequency Distributions, Conditional Frequency Distributions, Stopwords

  • Frequency Distributions
  • Conditional Frequency Distributions
  • Stopwords



import nltk

from nltk.corpus import stopwords


# CNN 기사 내용중 일부.

# https://edition.cnn.com/2018/11/07/politics/matthew-whitaker-attorney-general-mueller-special-counsel-probe/index.html


sent = 'The man taking over the Justice Department following Jeff Sessions firing as attorney general has argued that special counsel Robert Muellers investigation went too far. Matthew Whitaker, who was Sessions chief of staff, is expected to take over oversight of Muellers investigation into Russian interference in the 2016 election and whether Trump campaign associates colluded with Russia.'


# 워드 토큰 생성.

origin_words = nltk.word_tokenize(sent)


# Stopwords 지워주기

words = [w for w in origin_words if not w in stopwords.words('english')]


# Frequency Distributions

fd = nltk.FreqDist(words)


print("origin words count : ",len(origin_words))

print("After Stopword : ",len(words))

print("items >> ", fd.items())

print("keys >> ", fd.keys())

print("values >> ", fd.values())


fd.plot()



# 단어와 카테고리를 지정.

import nltk

from nltk.corpus import reuters


# 장르 및 단어 설정.

genres_list = ['alum','cpu','ship','rand','tea']

words_list = ['smelting','BANK','CAPACITY','PORTS','Brooke']

# 해당 카테고리, 단어 추출.

gwlist = [(genre,word) for genre in reuters.categories() if genre in genres_list for word in reuters.words(categories=genre) if word in words_list]


cfd = nltk.ConditionalFreqDist(gwlist)

cfd.tabulate()

cfd.plot()



+ Recent posts