본문 바로가기

반응형

Deep Learning/Tensorflow

(32)
Tensorflow - call backs 만들기 ( 가장 좋은 모델을 자동저장, 로그도 자동저장) call backs # python으로 dir 만드는 코드 if not os.path.exists(PROJECT_PATH + '/checkpoints/' + model_type + '/') : os.makedirs(PROJECT_PATH + '/checkpoints/' + model_type + '/') if not os.path.exists(PROJECT_PATH + '/log/' + model_type + '/') : os.makedirs(PROJECT_PATH + '/log/' + model_type + '/') from tensorflow.keras.callbacks import ModelCheckpoint cp = ModelCheckpoint( CHECKPOINT_PATH, monitor = '..
Tensorflow - Fine Tuning Fine Tuning Fine Tuning은 Transfer Learning을 한 상태에서 base_model과 head_model을 하나로 합친 model의 base_model 특정 layer를 지정해서 Frozen을 해제해주고 해제한 layer를 추가 학습시켜 정확도를 높이는 방법 Fine Tuning 은 꼭 !!! Transfer Learning을 한 상태에서 수행하는 방법이다. # 1. base_model의 전체 레이어를 학습 가능토록 먼저 바꿔준다. base_model.trainable = True # 2. base_model의 전체 레이어 수를 확인한다. len( base_model.layers ) # 지정한 특정 레이어까지 Frozen하기 # 3. 몇번째 레이어/가지 학습이 안되도록 할 지 ..
Tensorflow - Transfer Learning Transfer Learning 특징을 잡아내는데에 잘만들어진 모델을 활용하는걸 Transfer_Learning이라고 한다. 컨볼루션과 풀링으로 이루어진 특징을잡아내는 CNN모델을 Base Model 분류하는데 사용한 ANN모델을 Head Model 이라고 한다. import os import zipfile import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 파이썬의 진행상태를 표시해 주는 라이브러리 from tqdm import tqdm_notebook from tensorflow.keras.preprocessing.image import ImageDataGenerator %matplotlib inline tf.__v..
Tensorflow - numpy array로 되어있는 데이터를 증강하는 방법 numpy array로 되어있는 데이터를 증강하는 방법 # 데이터 불러오기 (X_train, y_train), (X_test, y_test) = cifar10.load_data() # 피처스케일링 X_train = X_train / 255.0 X_test = X_test / 255.0 y_train = to_categorical( y_train, 10) y_test = to_categorical( y_test, 10 ) # 모델링 (모델링 함수 적용) model = build_model() # 컴파일 model.compile('rmsprop', 'categorical_crossentropy', ['accuracy']) # 데이터를 증강해서 학습시킬것이다. 넘파이 어레이로 되어있는 데이터를 증강하는 방법 ..
Tensorflow - 파일을 Traning과 Test 디렉토리로 나눠서 저장하는 방법 # /tmp 경로에 압축파일을 다운로드 받고 압축 푼다. !wget --no-check-certificate \ "https://block-edu-test.s3.ap-northeast-2.amazonaws.com/kagglecatsanddogs_5340.zip" \ -O "/tmp/cats-and-dogs.zip" local_zip = '/tmp/cats-and-dogs.zip' zip_ref = zipfile.ZipFile(local_zip, 'r') zip_ref.extractall('/tmp') zip_ref.close() # /tmp 디렉토리 안에다, 학습을 위한 데이터를 분류하기 위해 # cats-v-dogs 디렉토리를 만들고, # 그 아래 training 과 testing 디렉토리 만든 후 #..
Tensorflow - ImageDataGenerator를 이용해서 데이터 증강하는 법 Image Augmentation 사용 코드 예시 TRAINING_DIR = '/tmp/cats-v-dogs/training' train_datagen = ImageDataGenerator( rescale = 1 / 255.0, rotation_range = 30, width_shift_range = 0.2, height_shift_range = 0.2, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True) train_generator = train_datagen.flow_from_directory( TRAINING_DIR, target_size = (300,300), class_mode = 'binary', batch_size = 20 ) VALI..
Tensorflow - os.mkdir 이용하여, 파일 저장할 다음 디렉토리 만들기 os.mkdir try: os.mkdir('/tmp/cats-v-dogs') os.mkdir('/tmp/cats-v-dogs/training') os.mkdir('/tmp/cats-v-dogs/testing') os.mkdir('/tmp/cats-v-dogs/training/cats') os.mkdir('/tmp/cats-v-dogs/training/dogs') os.mkdir('/tmp/cats-v-dogs/testing/cats') os.mkdir('/tmp/cats-v-dogs/testing/dogs') except OSError: pass
Tensorflow - Colab에 file upload해서 모델에 적용하는 함수 # 구글 코랩에 파일 업로드해서 모델에 적용하는 함수 import numpy as np from google.colab import files from tensorflow.keras.preprocessing import image uploaded = files.upload() for fn in uploaded.keys() : path = '/content/' + fn img = image.load_img(path, target_size=(300,300)) x = image.img_to_array(img) / 255.0 print(x.shape) x = np.expand_dims(x, axis = 0) print(x.shape) images = np.vstack( [x] ) classes = model.p..

반응형