본 내용은 fastcampus 딥러닝/인공지능 올인원 패키지 online을 정리한 것이다.
이미지 전처리
augmentation
python glob # 경로 너주면 한번에 읽어오기
os.lisdir() 과 glob차이 :
os.listdir() => 하위에 있는 파일 다 읽어 온다. 파일 명만 가져온다.
glob.glob() => 필요한 파일 가져올 수 있다.
데이터 분석
How to download MNIST images as PNGs?
import gzip
f = gzip.open('./dataset/MNIST/raw/train-images-idx3-ubyte.gz','r')
image_size = 28
num_images = 5
import numpy as np
import matplotlib.pyplot as plt
f.read(16)
buf = f.read(image_size * image_size * num_images)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, image_size, image_size, 1)
image = np.asarray(data[2]).squeeze()
plt.imshow(image)
stackoverflow.com/questions/55049511/how-to-download-mnist-images-as-pngs
How to download MNIST images as PNGs
I want to download the MNIST images to my computer as PNG files. I found this page: http://yann.lecun.com/exdb/mnist/ After I pressed: train-images-idx3-ubyte.gz: training set images (9912422 bytes)
stackoverflow.com
github.com/myleott/mnist_png/blob/master/convert_mnist_to_png.py
myleott/mnist_png
MNIST converted to PNG format. Contribute to myleott/mnist_png development by creating an account on GitHub.
github.com
import os
import struct
import sys
from array import array
from os import path
import png
# source: http://abel.ee.ucla.edu/cvxopt/_downloads/mnist.py
def read(dataset = "training", path = "."):
if dataset is "training":
fname_img = os.path.join(path, 'train-images-idx3-ubyte')
fname_lbl = os.path.join(path, 'train-labels-idx1-ubyte')
elif dataset is "testing":
fname_img = os.path.join(path, 't10k-images-idx3-ubyte')
fname_lbl = os.path.join(path, 't10k-labels-idx1-ubyte')
else:
raise ValueError("dataset must be 'testing' or 'training'")
flbl = open(fname_lbl, 'rb')
magic_nr, size = struct.unpack(">II", flbl.read(8))
lbl = array("b", flbl.read())
flbl.close()
fimg = open(fname_img, 'rb')
magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
img = array("B", fimg.read())
fimg.close()
return lbl, img, size, rows, cols
def write_dataset(labels, data, size, rows, cols, output_dir):
# create output directories
output_dirs = [
path.join(output_dir, str(i))
for i in range(10)
]
for dir in output_dirs:
if not path.exists(dir):
os.makedirs(dir)
# write data
for (i, label) in enumerate(labels):
output_filename = path.join(output_dirs[label], str(i) + ".png")
print("writing " + output_filename)
with open(output_filename, "wb") as h:
w = png.Writer(cols, rows, greyscale=True)
data_i = [
data[ (i*rows*cols + j*cols) : (i*rows*cols + (j+1)*cols) ]
for j in range(rows)
]
w.write(h, data_i)
input_path = './dataset/MNIST/raw/'
output_path = './dataset/MNIST/pngFile/'
for dataset in ["training", "testing"]:
labels, data, size, rows, cols = read(dataset, input_path)
write_dataset(labels, data, size, rows, cols,
path.join(output_path, dataset))
label 가져오기
def get_label(path):
class_name = path.split('/')[-2]
label = int(class_name)
return label
get_label(path)
h, w = image.shape
from tqdm import tqdm_notebook => 어디 까지 진행했는지 알 수 있다.
for path in tqdm_notebook(data_paths): => 사용법
plt평균값 보여주기
plt.axvline(np.mean(heights), color='r', linestyle='dashed' , linewidth = 2)
unique 한 값 찾기
np.unique(heights)
메모리 문제 때문에 batch size만큼 짤라서 번갈아가면서 넣어준다.
4차원(batch, height, width, channel)
ImageDataGenerator
width_shift_range => 좌우로 변동 0.2 라면 0.2 range만큼 랜덤으로 바꾼다.
zoom_range => 위아래 좌우 늘리기 좌우 늘리기
rescale => data normalization
ImageDataGenerator를 할 때는 fit_generator() -> 로 학습한다.
unique 한 개수 도 출력
np.unique(class_names,return_counts = True )
DataFrame
pd.DataFrame() => 데이터 생성
h,w,c = image.shape
tensorflow 함수로 label얻기
tf.strings.split(path, '_')[-1]
tensorflow image labeling
cls_name = tf.strings.regex_replace(fname, '.png', '')
cls_name
one_hot_encoding = tf.cast(class_names == cls_name, tf.unit8)
tqdm_notebook을 사용하면 진행도를 수 있다.
예:
tqdm_notebook 사용법
from tqdm import tqdm_notebook
for path in tqdm_notebook(data_paths):
img_pil = Image.open(path)
image = np.array (img_pil)
h, w = image.shape
heights.append(h)
widths.append(w)
tensorflow unique
classes = tf.unique(class_names).y.numpy()
classes
python current time
datetime.now()
history.params => history 파라미터 확인
tensorboard로 하면 속도가 늦어 질 수 있다.
history 로 볼수 있는데 실시간으로 는 볼수 가 없다.
tensorflow Checkpoint
tf.keras.callbacks.ModelCheckpoint(save_path, monitor='val_loss')
save model
model.save()
model = tf.keras.models.load_model('')
transfor-leraning을 위해서
weights저장 가능
model.save_weights('**.h5')
model.load_weights()
json으로 저장도 가능
with open('**.json','w') as f:
f.write(model.to_json())
h5모델 들여다보기
import h5py
model_file = h5py.File('**.h5','r+')
model_file.keys()
model_file['*.h5'].keys()
'교육동영상 > 01. 딥러닝인공지능' 카테고리의 다른 글
08. 딥러닝 기초 (0) | 2020.12.02 |
---|---|
07. 이미지 분석 pytorch (0) | 2020.12.01 |
05. Tensorflow 2.0 Pytorch (0) | 2020.11.24 |
04. 인공지능에 대한 개념과 준비 (0) | 2020.11.23 |
03. python 함수 (0) | 2020.11.19 |