기본 콘텐츠로 건너뛰기

[연구내용정리] 코드정리(utils_min.py)

필요한 라이브러리

import cv2
import numpy as np
import scipy.io
import os, sys
import glob
cv2 : 영상을 읽을 때 opencv를 사용함 scipy.io : matlab에서 구한 결과값을 사용하기 위해 mat-file을 load해야함 glob : 폴더 내의 영상들의 리스트를 읽어올 때 필요한 라이브러리 os, sys : 경로추가 때 필요한 라이브러리 image파일들이 포함된 폴더에 접근하여 폴더 내의 모든 image파일 리스트를 가져오고 cv2.imread를 이용하여 image에 포함된 픽셀 값을 담을 수 있는 변수를 생성한다. image의 전처리 과정들은 numpy 라이브러리르 많이 사용하기 때문에, array형태로 저장한다. 그 결과 image 파일 리스트와 image가 담긴 변수를 반환한다.

def load_img_list(path, extension_name):    



    if os.path.exists(path + '/*.' + extension_name):        



    print("No File")
    img_file_list = glob.glob(path + '/*.' + extension_name)
    ori_img = []

    for i in range(len(img_file_list)):        



        img = cv2.imread(img_file_list[i], cv2.IMREAD_GRAYSCALE)
        ori_img.append(img)

    ori_img = np.array(ori_img)

    return ori_img, img_file_list
matlab에서 실행하면 결과값이 mat파일로 저장된다. 이를 불러오고, mat파일 내의 원하는 인자들을 들고 오기 위해 dictionary 값을 인자로 넣어준다. 본 연구에서는 x와 y값이 한번에 저장된 mat 파일을 불러오기 때문에, 분리하여 array형태로 저장할 수 있는 함수를 만들었다.

def load_mat_file_mod(save_filename, x_dic, y_dic):
    data = scipy.io.loadmat(save_filename + '.mat')
    x_data = data[x_dic]
    y_data = data[y_dic]

    arr_x_raw = np.array(x_data)
    arr_y_raw = np.array(y_data)

    return arr_x_raw, arr_y_raw

matlab에 digital image correlation 알고리즘 m-file에서는 마우스커서로 left-top과 right-bottom의 두 점을 찍고, grid의 간격 값을 입력하면 grid로 나눌 수 있었다. 이때 grid 좌표 값이 mat file로 출력되는데 이를 이용하여 python에서도 grid별로 image를 자르고 이를 저장하는 함수를 만들었다.

def crop_save_img_mod(original_image_array ,
                      gridx, gridy,
                      save_extension,
                      skip,
                      height = 32, width = 32):

    ori_img = original_image_array    

    grid_width = width    

    grid_height = height    

    ori_img = np.array(ori_img)


    crop_img_path = './cropped_imgs'
    crop_img = []
    for i in range(0, len(ori_img)-skip, skip):        

        crop_img_path_folder = crop_img_path + '%d' % i
        if not os.path.exists(crop_img_path_folder):            

              os.mkdir(crop_img_path_folder)
        for j in range(len(gridx)):            

              crop_img.append(ori_img[i+skip][
                            int(gridy[j]):int(gridy[j]) + grid_height,
                            int(gridx[j]):int(gridx[j]) + grid_width])
              tmp_filename = 'cropped' + '%d' % j + '.' + save_extension
데이터 중 outlier 데이터를 제거하기 위해서 다음과 같은 함수를 작성하였다. 정규분포에서 +-3sigma 보다 큰 데이터는 신뢰도 99.8%에서 벗어나기 때문에, 이러한 데이터들을 제거하였다. 하지만 sigma값을 다르게 줄 수 있도록 sigma값을 인자로 주었다.

def delete_outlier(data , thr_sigma):    mu = np.mean(data)
    sigma = np.std(data)
    print('mean : ', mu)
    print('standard deviation : ', sigma)

    bottom_thd = mu - thr_sigma * sigma
    top_thd = mu + thr_sigma * sigma

    return np.logical_and(np.greater_equal(data, bottom_thd), np.less_equal(data, top_thd))

댓글

이 블로그의 인기 게시물

데이터 열로 추가하여 쌓기

import numpy as np x = [] a = np.array([1,2,3]) Out[5]: array([1, 2, 3]) a.shape Out[6]: (3,) np.concatenate((x,a)) Out[7]: array([ 1.,  2.,  3.]) x = np.concatenate((x,a)) x = np.concatenate((x,a)) Out[10]: array([ 1.,  2.,  3.,  1.,  2.,  3.]) x = [] x = np.concatenate((x,a), axis=1) => 오류발생  -. x가 차원이 안맞아서 안됨.  np.expand_dims(a,axis=1) Out[14]:  array([[1],        [2],        [3]]) b = np.expand_dims(a,axis=1) b.shape Out[16]: (3, 1) => (3,1)짜리 차원으로 확장한 후 열 방향으로 쌓을 수 있다. => 아래는 empty함수나 zeros를 사용하여 공간을 만드는 작업 x = [] x = np.empty((3,0)) x Out[19]: array([], shape=(3, 0), dtype=float64) x = np.zeros((3,0)) x Out[21]: array([], shape=(3, 0), dtype=float64) x = np.zeros((3,0)) a Out[23]: array([1, 2, 3]) b Out[24]: array([[1],        [2],        [3]]) np.concatenate((x,b), axis=1) Out[26]: array([[ 1.], ...

HTML 테스트

Lab 시리즈 화장품 구매(파란색, 가장크게) <span style="color: blue; font-size: x-large;">Lab 시리즈 화장품 구매(파란색, 가장크게)</span><br /> Lab 시리즈 화장품 구매(검은색, 보통) <span style="color: blue; font-size: x-large;"><br /></span> Lab 시리즈 화장품 구매(검은색, 보통)<br /> Lab 시리즈 화장품 구매(검은색, 보통, Airal) <span style="font-family: Arial, Helvetica, sans-serif;">Lab 시리즈 화장품 구매(검은색, 보통, Airal)</span><br /> Lab 시리즈 화장품 구매(검은색, 보통, Airal, Bold) <span style="font-family: Arial, Helvetica, sans-serif;"><b>Lab 시리즈 화장품 구매(검은색, 보통, Airal, Bold)</b></span> 하나의 문단을 의미하는 <p>태그 문장의 일부를 의미하는 <span>태그 강제줄바꿈을 의미하는 <br>태그 비교적 넓은 지역을 묶어 지정 </div>

keras를 이용하여 cifar10 실행해보기 (Functional API step by step)

Keras 라이브러리를 이용하여 cifar 10 데이터를 기본적인 CNN 구조를 이용하여 수행 사용하는 레이어들을 추가한다. from keras.layers import Input, Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D 모델도 추가한다. from keras.models import Model 구조는 기본적으로 아래와 같다. 이는 keras에서 제공하는 cifar10 예제를 참고하였다. conv2D -> conv2D -> maxpooling -> dropout -> conv2D -> conv2D -> maxpooling -> dropout -> flatten -> Dense -> dropout ->Dense(마지막은 분류하고자 하는 개수, 여기서는 10) 블럭을 생성하기 전에 Input layer에 형태를 넣어줘야한다. #Input inputs =Input((32,32,3)) # block1 x = Conv2D(32, (3,3), activation='relu', padding='same', name='block1_conv1')(inputs)  x = Conv2D(32, (3,3), activation='relu', padding='valid', name='block1_conv2')(x)  x = MaxPooling2D((2, 2), name='block1_pool')(x) x = Dropout(0.25)(x)  # block2 x = Conv2D(64, (3,3), activation='relu', padding='same', name='block2_conv1')...