참조사이트 : https://blog.hbsmith.io/c-%EA%B8%B0%EB%B0%98-semantic-segmentation-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%A8-%EA%B0%9C%EB%B0%9C-d330a98a005b

 

* CUDA & CUDNN 환경

* CUDA : 11.3
* CUDNN : 8.2.1

1. 다음의 명령어로 가상환경을 만든다

conda create -n segmentation python=3.9
conda activate segmentation

2. github로부터 다운 받는다

git clone https://github.com/qubvel/segmentation_models.pytorch

3. 생성된 폴더로 이동한 뒤 필요한 녀석들을 설치한다.

cd segmentation_models.pytorch
pip install -r requirements.txt

4. GPU 버전을 설치하기 위해 다음의 항목들을 지우고 다시 설치한다.

(https://pytorch.org/ 참조)

pip uninstall torch
pip uninstall torchvision

pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113

5. 추가로 필요한 기능들을 설치한다

pip install matplotlib
pip install pytorch-lightning

6. Command Line 상에서 다음과 같이 가상환경 위치를 찾아낸다.

7. 가상환경의 경로에  path.pth 라는 텍스트 파일을 만든 뒤 github로부터 다운받은 루트폴더 경로를 저장한다

* 파일명 : d:\Anaconda3\envs\segmentation\path.pth
* 내용 --------------------------------------------------
D:\DeepLearning\Repository\segmentation_models.pytorch
---------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

이미지의 경계선을 그려주는 모델같은데..... 테스트 해봄

 

1. tensorflow 1.15.4 버전에서 구동할거라

   - cuda : 10.0

   - cudnn : 7.4로 맞춤

 

2. anaconda 에서 환경을 만든다

conda create -n dexined python=3.7
conda activate dexined

 

3. 활성화 시킨 다음 tensorflow설치한다

pip install tensorflow-gpu==1.15.4

 

4. github에서 소스를 받은 후 legacy폴더로 이동

git clone https://github.com/xavysp/DexiNed
cd DexiNed/legacy

 

5. requirements.txt파일을 이용하여 설치 시작해야 하는데, 그냥 설치하면 다음과 같이 오류 발생

** 그러나 그대로 설치하면 다음과 같이 오류 발생

requirements.txt파일을 다음과 같이 수정

....
grpcio==1.26.0
h5py==2.10.0
idna==2.9
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
Markdown==3.1.1
matplotlib==3.1.2
mock==4.0.2
numpy==1.18.1
oauthlib==3.1.0
opencv-python==4.4.0   =(수정)=> opencv-python==4.4.0.46
opt-einsum==3.2.1
pkg-resources==0.0.0    <== 삭제
protobuf==3.11.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pyparsing==2.4.6
python-dateutil==2.8.1
requests==2.23.0
requests-oauthlib==1.3.0
.....

후에 다음과 같이 설치

pip install -r requirements.txt

 

6. 추가로 다음을 설치해준다

pip install pillow

 

7. 다음을 실행하면 오류 발생한다

python run_model.py --image_width=512 --image_height=512

8. 아무이름.pth파일을 만들어서 github를 다운받은 경로를 넣고

'F:\D\DL\DexiNed'

[Anaconda설치경로]\envs\dexined\Lib\site-packages 에 복사한다

** https://doitgrow.com/24 참조

 

9. https://drive.google.com/drive/folders/1fLBpOrSXC2VOWUvDtNGyrHcuB2IB-4_D 경로에서 파일 4개를 다운받는다

1) train_2.zip파일을 풀어서 legacy/checkpoints/DXN_BIPED/train 폴더를 차례로 만들어서 넣어준다

 2) data_list.zip파일을 풀어서 legacy/data폴더를 만들고 그 안에 데이터 파일을 넣는다

10. 다음의 명령어로 테스트를 진행한다

python run_model.py --image_width=512 --image_height=512 --model_state=test

뭔가 진행이 된다.

** legacy\results\edges\DexiNed_BIPED2CLASSIC 폴더에 4개의 폴더가 생성되고, 각각의 폴더에 결과가 저장되어 있다

 

 

1. Pytorch 최신 버전으로 설치하려고 하니 내 PC에 설치된 cuda 10.2는 더이상 지원하지 않는다고 하여  11.3설치

2. ananconda 가상환경 생성 및 설치

> conda create -n mmdetection python=3.7
> conda activate mmdetection
> conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
> pip install onnx
> cd mmdetection
> python setup.py install
> pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu113/torch1.11.0/index.html

3. 작업할 폴더로 이동한 뒤 mmdetection를 다운받는다

git clone https://github.com/open-mmlab/mmdetection

4. faster rcnn 사전 학습된 파일을 다음의경로에서 다운 받은 뒤 현재 작업 폴더로 복사한다

http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth 
 

 

5. 다음의 명령어로 pth파일을 onnx파일로 변경한다

python tools/deployment/pytorch2onnx.py \
configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
../faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \
--output-file ../faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.onnx \
--input-img demo/demo.jpg --test-img demo/demo.jpg  \
--shape 608 608 \
--show \
--verify \
--dynamic-export \
--cfg-options \
model.test_cfg.deploy_nms_pre=-1

1. 다음과 같이 작성한다.

balloon_freeze.py

import os
import sys

ROOT_DIR = os.path.abspath("../../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library

from mrcnn.config import Config
from keras import backend as k
from tensorflow.python.framework.graph_util_impl import convert_variables_to_constants
import tensorflow as tf
import mrcnn.model as modellib

class outConfig(Config):
    NAME = "balloon"
    NUM_CLASSES = 1 + 1
    BACKBONE = "resnet101"
    IMAGE_MIN_DIM = 800
    IMAGE_MAX_DIM = 1024
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
    DETECTION_MAX_INSTANCES = 100

MODEL_DIR = os.path.join(ROOT_DIR, "model/balloon/logs") #모델 저장 폴더
out_config = outConfig()
out_config.display()

model = modellib.MaskRCNN(mode="inference", config=out_config,model_dir=MODEL_DIR)
model_path = model.find_last()
model.load_weights(model_path, by_name=True)

session = k.get_session()

min_graph = convert_variables_to_constants(session,session.graph_def,[out.op.name for out in model.keras_model.outputs])
tf.train.write_graph(min_graph, './', 'mrcnn.pb', as_text=False)

outConfig의 내용은 이전 inference 에서 나왔던 값들과 동일하게 맞춤

(그게 맞는지는 모름 -_-)

 

* 실행 성공하면 mrcnn.pb파일이 생성됨

 

 

2. 로드 후 테스트 하려면 

1) MASK_RCNN/mrcnn/model.py 

  - MaskRCNN클래스에 다음의 멤버함수를 추가한다

def detect_pb(self, images, sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask, verbose=1):
        """Runs the detection pipeline.

        images: List of images, potentially of different sizes.

        Returns a list of dicts, one dict per image. The dict contains:
        rois: [N, (y1, x1, y2, x2)] detection bounding boxes
        class_ids: [N] int class IDs
        scores: [N] float probability scores for the class IDs
        masks: [H, W, N] instance binary masks
        """
        assert self.mode == "inference", "Create model in inference mode."
        assert len(
            images) == self.config.BATCH_SIZE, "len(images) must be equal to BATCH_SIZE"

        # if verbose:
        #     log("Processing {} images".format(len(images)))
        #     for image in images:
        #         log("image", image)

        # Mold inputs to format expected by the neural network
        molded_images, image_metas, windows = self.mold_inputs(images)

        #print('molded_images : ', molded_images)
        #print('image_metas : ', image_metas)
        #print('windows : ', windows)

        # Validate image sizes
        # All images in a batch MUST be of the same size
        image_shape = molded_images[0].shape
        # print(image_shape, molded_images.shape)
        for g in molded_images[1:]:
            assert g.shape == image_shape,\
                "After resizing, all images must have the same size. Check IMAGE_RESIZE_MODE and image sizes."

        # Anchors
        anchors = self.get_anchors(image_shape)
        # Duplicate across the batch dimension because Keras requires it
        # TODO: can this be optimized to avoid duplicating the anchors?
        anchors = np.broadcast_to(anchors, (self.config.BATCH_SIZE,) + anchors.shape)

        # if verbose:
        #     log("molded_images", molded_images)
        #     log("image_metas", image_metas)
        #     log("anchors", anchors)
        # Run object detection
        # detections, _, _, mrcnn_mask, _, _, _ =\
        #     self.keras_model.predict([molded_images, image_metas, anchors], verbose=0)
        detectionsed, mrcnn_masked = sessd.run([detections, mrcnn_mask], feed_dict = {input_image: molded_images, \
                                                                input_image_meta: image_metas, \
                                                                input_anchors: anchors})

        print('detectionsed : ', detectionsed.shape)
        print('mrcnn_masked : ', mrcnn_masked.shape)
        mrcnn_masked = np.expand_dims(mrcnn_masked, 0)
        detections = np.array(detectionsed)
        mrcnn_mask = np.array(mrcnn_masked)
        
        print('detections : ', detections.shape)
        print('mrcnn_mask : ', mrcnn_mask.shape)
        
        # Process detections
        results = []
        for i, image in enumerate(images):
            xi = detections[i]
            yi = mrcnn_mask[i]
            moldedi = molded_images[i]
            windowsi = windows[i]
            final_rois, final_class_ids, final_scores, final_masks =\
                self.unmold_detections(detections[i], mrcnn_mask[i],
                                        image.shape, molded_images[i].shape,
                                        windows[i])
            results.append({
                "rois": final_rois,
                "class_ids": final_class_ids,
                "scores": final_scores,
                "masks": final_masks,
            })
        return results

 

3. 생성된 pb파일을 테스트하기 위해서 다음과 같이 작성

import os
import sys

ROOT_DIR = os.path.abspath("../../")
sys.path.append(ROOT_DIR)  # To find local version of the library


import cv2
import tensorflow as tf
import mrcnn.model as modellib
from mrcnn.config import Config

class outConfig(Config):
    NAME = "balloon"
    NUM_CLASSES = 1 + 1
    BACKBONE = "resnet101"
    IMAGE_MIN_DIM = 800
    IMAGE_MAX_DIM = 1024
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
    DETECTION_MAX_INSTANCES = 100

MODEL_DIR = os.path.join(ROOT_DIR, "model/balloon/logs") #모델 저장 폴더

out_config = outConfig()
out_config.display()
#results = model.detect([img], verbose=1)

def load_detection_model(model):
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(model, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
        input_image = tf.get_default_graph().get_tensor_by_name('input_image:0')
        input_image_meta = tf.get_default_graph().get_tensor_by_name('input_image_meta:0')
        input_anchors = tf.get_default_graph().get_tensor_by_name('input_anchors:0')
        detections = tf.get_default_graph().get_tensor_by_name('mrcnn_detection/Reshape_1:0')
        #mrcnn_mask = tf.get_default_graph().get_tensor_by_name('mrcnn_mask/Sigmoid:0')
        mrcnn_mask = tf.get_default_graph().get_tensor_by_name('mrcnn_mask/Sigmoid:0')
    sessd=tf.Session(config=config,graph=detection_graph)
    print('Loaded detection model from file "%s"' % model)
    return sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask

 

model = modellib.MaskRCNN(mode="inference", config=out_config,model_dir=MODEL_DIR)

model_path = 'mrcnn.pb'

image = cv2.imread("D:/DL/Mask_RCNN/model/balloon/datasets/train/53500107_d24b11b3c2_b.jpg")
image = cv2.resize(image,(1024,1024),0,0,cv2.INTER_AREA)
sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask = load_detection_model(model_path)
results = model.detect_pb([image], sessd, input_image, input_image_meta, input_anchors, detections, mrcnn_mask,verbose=1)

#print('rois : ' , results[0]['rois'])
#print('class_ids : ' , results[0]['class_ids'])
#print('scores  : ', results[0]['scores'])
#print('masks  : ', results[0]['masks'].shape)

print(results[0]['masks'].shape)

 

 

 

 

 

 

 

 

 

 

학습시에 특정 시점에 호출될 함수를 등록할 수 있다

 

1.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)

  - 학습 중간중간에 저장할 수 있다.

  - filepath: filepath는 (on_epoch_end에서 전달되는) epoch의 값과 logs의 키로 채워진 이름 형식 옵션을 가질 수 있음.
예를 들어 filepath가 weights.{epoch:02d}-{val_loss:.2f}.hdf5라면, 파일 이름에 세대 번호와 검증 손실을 넣어 모델의 체크포인트가 저장 
* monitor: 모니터할 지표(loss 또는 평가 지표) 

  - val_loss : 줄어드는 것을 모니터링

  - val_accuracy : 커지는 것을 모니터링
* save_best_only: 가장 좋은 성능을 나타내는 모델만 저장할 여부
* save_weights_only: Weights만 저장할 지 여부 

  - True : weight와 bias 값만 저장한다

  - False : layer 구성, 노드 갯수, 노드 정보, activation 정보 등의 값을 같이 저장한다

  - 참고) True를 권장 , SaveWeights / LoadWeights
* mode: {auto, min, max} 중 하나. monitor 지표가 감소해야 좋을 경우 min, 증가해야 좋을 경우 max, auto는 monitor 이름에서 자동으로 유추. 

  - monitor가 val_loss인 경우 min

  - monitor가 val_accuracy인 경우 max

* period 확인 횟수 : 1이면 매번 1epoch시 확인, 3이면 3epoch시 확인

from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.optimizers import Adam

model = create_model()
model.compile(optimizer=Adam(0.001), loss='categorical_crossentropy', metrics=['accuracy'])

mcp_cb = ModelCheckpoint(filepath='/kaggle/working/weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_loss', 
                         save_best_only=True, save_weights_only=True, mode='min', period=3, verbose=1)
history = model.fit(x=tr_images, y=tr_oh_labels, batch_size=128, epochs=10, validation_data=(val_images, val_oh_labels),
                   callbacks=[mcp_cb])

> 결과

Epoch 1/10
399/399 [==============================] - 3s 5ms/step - loss: 0.6208 - accuracy: 0.7874 - val_loss: 0.4639 - val_accuracy: 0.8360
Epoch 2/10
399/399 [==============================] - 2s 4ms/step - loss: 0.4271 - accuracy: 0.8485 - val_loss: 0.4034 - val_accuracy: 0.8553
Epoch 3/10
399/399 [==============================] - 2s 4ms/step - loss: 0.3787 - accuracy: 0.8646 - val_loss: 0.3800 - val_accuracy: 0.8649

Epoch 00003: val_loss improved from inf to 0.37997, saving model to /kaggle/working/weights.03-0.38.hdf5
Epoch 4/10
399/399 [==============================] - 2s 4ms/step - loss: 0.3516 - accuracy: 0.8738 - val_loss: 0.3673 - val_accuracy: 0.8676
Epoch 5/10
399/399 [==============================] - 2s 5ms/step - loss: 0.3331 - accuracy: 0.8798 - val_loss: 0.3420 - val_accuracy: 0.8778
Epoch 6/10
399/399 [==============================] - 2s 4ms/step - loss: 0.3127 - accuracy: 0.8871 - val_loss: 0.3651 - val_accuracy: 0.8716

Epoch 00006: val_loss improved from 0.37997 to 0.36512, saving model to /kaggle/working/weights.06-0.37.hdf5
Epoch 7/10
399/399 [==============================] - 2s 4ms/step - loss: 0.3005 - accuracy: 0.8905 - val_loss: 0.3428 - val_accuracy: 0.8756
Epoch 8/10
399/399 [==============================] - 2s 4ms/step - loss: 0.2852 - accuracy: 0.8963 - val_loss: 0.3354 - val_accuracy: 0.8769
Epoch 9/10
399/399 [==============================] - 2s 4ms/step - loss: 0.2810 - accuracy: 0.8963 - val_loss: 0.3265 - val_accuracy: 0.8813

Epoch 00009: val_loss improved from 0.36512 to 0.32655, saving model to /kaggle/working/weights.09-0.33.hdf5
Epoch 10/10
399/399 [==============================] - 2s 5ms/step - loss: 0.2694 - accuracy: 0.9007 - val_loss: 0.3181 - val_accuracy: 0.8841

 

*** 참고로 jupyter notebook에서는 명령어는 느낌표(!) 뒤에 붙이면 된다

!ls -lia
#!rm -rf weight*
#!ls -lia

 

2.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)
* 특정 epochs 횟수동안 성능이 개선 되지 않을 시 Learning rate를 동적으로 감소 시킴 
* monitor: 모니터할 지표(loss 또는 평가 지표) 
* factor: 학습 속도를 줄일 인수. new_lr = lr * factor 
* patience: Learing Rate를 줄이기 전에 monitor할 epochs 횟수. 
* mode: {auto, min, max} 중 하나. monitor 지표가 감소해야 좋을 경우 min, 증가해야 좋을 경우 max, auto는 monitor 이름에서 유추. 

*** ex)val_loss가 감소하다가 특정구간에서 patience 횟수만큼 감소하지 않고 답보하거나 증가하면 learning rate에 factor를 곱해서 적용한다 

> 코드

from tensorflow.keras.callbacks import ReduceLROnPlateau

model = create_model()
model.compile(optimizer=Adam(0.001), loss='categorical_crossentropy', metrics=['accuracy'])

rlr_cb = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=3, mode='min', verbose=1)
history = model.fit(x=tr_images, y=tr_oh_labels, batch_size=128, epochs=30, validation_data=(val_images, val_oh_labels),
                   callbacks=[rlr_cb])

> 결과(30번의 epoch를 돌리는데 val_loss가 3번이 상 감소하지 않으면 learning_rate에 0.3을 곱한다

Epoch 1/30
399/399 [==============================] - 3s 5ms/step - loss: 0.6246 - accuracy: 0.7862 - val_loss: 0.4564 - val_accuracy: 0.8412
Epoch 2/30
399/399 [==============================] - 2s 5ms/step - loss: 0.4167 - accuracy: 0.8547 - val_loss: 0.4136 - val_accuracy: 0.8520
Epoch 3/30
399/399 [==============================] - 2s 5ms/step - loss: 0.3772 - accuracy: 0.8668 - val_loss: 0.3806 - val_accuracy: 0.8647
Epoch 4/30
399/399 [==============================] - 2s 5ms/step - loss: 0.3506 - accuracy: 0.8757 - val_loss: 0.3913 - val_accuracy: 0.8581
Epoch 5/30
399/399 [==============================] - 2s 5ms/step - loss: 0.3294 - accuracy: 0.8816 - val_loss: 0.3576 - val_accuracy: 0.8702
Epoch 6/30
399/399 [==============================] - 2s 6ms/step - loss: 0.3130 - accuracy: 0.8857 - val_loss: 0.3492 - val_accuracy: 0.8758
Epoch 7/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2984 - accuracy: 0.8930 - val_loss: 0.3354 - val_accuracy: 0.8804
Epoch 8/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2905 - accuracy: 0.8931 - val_loss: 0.3288 - val_accuracy: 0.8813
Epoch 9/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2795 - accuracy: 0.8974 - val_loss: 0.3295 - val_accuracy: 0.8799
Epoch 10/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2694 - accuracy: 0.9016 - val_loss: 0.3254 - val_accuracy: 0.8834
Epoch 11/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2611 - accuracy: 0.9036 - val_loss: 0.3229 - val_accuracy: 0.8806
Epoch 12/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2530 - accuracy: 0.9064 - val_loss: 0.3449 - val_accuracy: 0.8756
Epoch 13/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2490 - accuracy: 0.9086 - val_loss: 0.3321 - val_accuracy: 0.8802
Epoch 14/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2432 - accuracy: 0.9095 - val_loss: 0.3236 - val_accuracy: 0.8847

Epoch 00014: ReduceLROnPlateau reducing learning rate to 0.0003000000142492354.
Epoch 15/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2120 - accuracy: 0.9214 - val_loss: 0.3065 - val_accuracy: 0.8904
Epoch 16/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2076 - accuracy: 0.9241 - val_loss: 0.3047 - val_accuracy: 0.8919
Epoch 17/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2047 - accuracy: 0.9248 - val_loss: 0.3078 - val_accuracy: 0.8909
Epoch 18/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2013 - accuracy: 0.9267 - val_loss: 0.3150 - val_accuracy: 0.8891
Epoch 19/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1986 - accuracy: 0.9277 - val_loss: 0.3108 - val_accuracy: 0.8904

Epoch 00019: ReduceLROnPlateau reducing learning rate to 9.000000427477062e-05.
Epoch 20/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1894 - accuracy: 0.9317 - val_loss: 0.3063 - val_accuracy: 0.8922
Epoch 21/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1876 - accuracy: 0.9318 - val_loss: 0.3069 - val_accuracy: 0.8938
Epoch 22/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1866 - accuracy: 0.9325 - val_loss: 0.3051 - val_accuracy: 0.8918

Epoch 00022: ReduceLROnPlateau reducing learning rate to 2.700000040931627e-05.
Epoch 23/30
399/399 [==============================] - 2s 6ms/step - loss: 0.1834 - accuracy: 0.9344 - val_loss: 0.3054 - val_accuracy: 0.8933
Epoch 24/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1829 - accuracy: 0.9338 - val_loss: 0.3052 - val_accuracy: 0.8932
Epoch 25/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1825 - accuracy: 0.9343 - val_loss: 0.3046 - val_accuracy: 0.8926

Epoch 00025: ReduceLROnPlateau reducing learning rate to 8.100000013655517e-06.
Epoch 26/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1815 - accuracy: 0.9350 - val_loss: 0.3053 - val_accuracy: 0.8923
Epoch 27/30
399/399 [==============================] - 2s 6ms/step - loss: 0.1813 - accuracy: 0.9349 - val_loss: 0.3053 - val_accuracy: 0.8923
Epoch 28/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1812 - accuracy: 0.9351 - val_loss: 0.3053 - val_accuracy: 0.8928

Epoch 00028: ReduceLROnPlateau reducing learning rate to 2.429999949526973e-06.
Epoch 29/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1808 - accuracy: 0.9351 - val_loss: 0.3054 - val_accuracy: 0.8923
Epoch 30/30
399/399 [==============================] - 2s 5ms/step - loss: 0.1808 - accuracy: 0.9349 - val_loss: 0.3054 - val_accuracy: 0.8924

 

3. EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None,                                          restore_best_weights=False)
특정 epochs 동안 성능이 개선되지 않을 시 학습을 조기에 중단
monitor: 모니터할 지표(loss 또는 평가 지표)
patience: Early Stopping 적용 전에 monitor할 epochs 횟수.
mode: {auto, min, max} 중 하나. monitor 지표가 감소해야 좋을 경우 min, 증가해야 좋을 경우 max, auto는 monitor 이름에서 유추.

* 예를 들어 loss는 계속 줄어드는데, val_loss는 늘어나는 경우

from tensorflow.keras.callbacks import EarlyStopping

model = create_model()
model.compile(optimizer=Adam(0.001), loss='categorical_crossentropy', metrics=['accuracy'])

ely_cb = EarlyStopping(monitor='val_loss', patience=3, mode='min', verbose=1)
history = model.fit(x=tr_images, y=tr_oh_labels, batch_size=128, epochs=30, validation_data=(val_images, val_oh_labels),
                   callbacks=[ely_cb])

> 결과 : val_loss가 3번 이상 떨어지지 않으면 중단

Epoch 1/30
399/399 [==============================] - 3s 6ms/step - loss: 0.5900 - accuracy: 0.7998 - val_loss: 0.4971 - val_accuracy: 0.8272
Epoch 2/30
399/399 [==============================] - 2s 5ms/step - loss: 0.4100 - accuracy: 0.8578 - val_loss: 0.4399 - val_accuracy: 0.8404
Epoch 3/30
399/399 [==============================] - 2s 5ms/step - loss: 0.3732 - accuracy: 0.8685 - val_loss: 0.4068 - val_accuracy: 0.8513
Epoch 4/30
399/399 [==============================] - 2s 5ms/step - loss: 0.3482 - accuracy: 0.8766 - val_loss: 0.3620 - val_accuracy: 0.8716
Epoch 5/30
399/399 [==============================] - 2s 6ms/step - loss: 0.3287 - accuracy: 0.8825 - val_loss: 0.3704 - val_accuracy: 0.8672
Epoch 6/30
399/399 [==============================] - 2s 5ms/step - loss: 0.3085 - accuracy: 0.8892 - val_loss: 0.3324 - val_accuracy: 0.8761
Epoch 7/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2992 - accuracy: 0.8906 - val_loss: 0.3522 - val_accuracy: 0.8734
Epoch 8/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2832 - accuracy: 0.8959 - val_loss: 0.3314 - val_accuracy: 0.8804
Epoch 9/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2745 - accuracy: 0.8997 - val_loss: 0.3185 - val_accuracy: 0.8823
Epoch 10/30
399/399 [==============================] - 2s 6ms/step - loss: 0.2674 - accuracy: 0.9015 - val_loss: 0.3168 - val_accuracy: 0.8850
Epoch 11/30
399/399 [==============================] - 2s 6ms/step - loss: 0.2581 - accuracy: 0.9038 - val_loss: 0.3485 - val_accuracy: 0.8728
Epoch 12/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2517 - accuracy: 0.9060 - val_loss: 0.3205 - val_accuracy: 0.8823
Epoch 13/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2423 - accuracy: 0.9096 - val_loss: 0.3110 - val_accuracy: 0.8883
Epoch 14/30
399/399 [==============================] - 2s 6ms/step - loss: 0.2386 - accuracy: 0.9115 - val_loss: 0.3262 - val_accuracy: 0.8877
Epoch 15/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2300 - accuracy: 0.9146 - val_loss: 0.3154 - val_accuracy: 0.8876
Epoch 16/30
399/399 [==============================] - 2s 5ms/step - loss: 0.2244 - accuracy: 0.9161 - val_loss: 0.3186 - val_accuracy: 0.8883
Epoch 00016: early stopping

 

 

* 사용예

  1) Callback 1 : Validation Loss가 향상되는 모델만 저장 

    - save_best_only : True

  2) Callback 2 : 5번 이내에 Loss가 향상되지 않으면 Learning Rate = 기존LR * 0.2

  3) Callback 3 : 10번 이내에 Loss가 향상되지 않으면 더 이상 학습하지 않고 종료

from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

model = create_model()
model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

# validation loss가 향상되는 모델만 저장.
mcp_cb = ModelCheckpoint(filepath='/kaggle/working/weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_loss', 
                         save_best_only=True, save_weights_only=True, mode='min', period=1, verbose=0)

# 5번 iteration내에 validation loss가 향상되지 않으면 learning rate을 기존 learning rate * 0.2로 줄임.  
rlr_cb = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, mode='min', verbose=1)
# 10번 iteration내에 validation loss가 향상되지 않으면 더 이상 학습하지 않고 종료
ely_cb = EarlyStopping(monitor='val_loss', patience=10, mode='min', verbose=1)


history = model.fit(x=tr_images, y=tr_oh_labels, batch_size=32, epochs=30, shuffle=True,
                    validation_data=(val_images, val_oh_labels),  
                    callbacks=[mcp_cb, rlr_cb, ely_cb] )

> 결과 (22번 iteration 후 LR 조정, 27번까지 돌고 종료)

Epoch 1/30
2021-11-08 01:31:08.029196: I tensorflow/stream_executor/cuda/cuda_dnn.cc:369] Loaded cuDNN version 8005
1329/1329 [==============================] - 16s 7ms/step - loss: 1.5707 - accuracy: 0.4319 - val_loss: 1.5818 - val_accuracy: 0.4725
Epoch 2/30
1329/1329 [==============================] - 8s 6ms/step - loss: 1.1025 - accuracy: 0.6091 - val_loss: 0.9318 - val_accuracy: 0.6757
Epoch 3/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.9168 - accuracy: 0.6804 - val_loss: 0.8748 - val_accuracy: 0.7053
Epoch 4/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.8169 - accuracy: 0.7155 - val_loss: 0.8552 - val_accuracy: 0.7063
Epoch 5/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.7370 - accuracy: 0.7476 - val_loss: 0.7564 - val_accuracy: 0.7397
Epoch 6/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.6601 - accuracy: 0.7747 - val_loss: 0.6641 - val_accuracy: 0.7764
Epoch 7/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.5948 - accuracy: 0.7965 - val_loss: 0.7745 - val_accuracy: 0.7393
Epoch 8/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.5477 - accuracy: 0.8139 - val_loss: 0.6274 - val_accuracy: 0.7904
Epoch 9/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.4969 - accuracy: 0.8302 - val_loss: 0.5946 - val_accuracy: 0.7999
Epoch 10/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.4542 - accuracy: 0.8427 - val_loss: 0.7064 - val_accuracy: 0.7689
Epoch 11/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.4284 - accuracy: 0.8537 - val_loss: 0.6313 - val_accuracy: 0.7969
Epoch 12/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.3928 - accuracy: 0.8657 - val_loss: 0.6357 - val_accuracy: 0.7984
Epoch 13/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.3600 - accuracy: 0.8757 - val_loss: 0.8772 - val_accuracy: 0.7440
Epoch 14/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.3359 - accuracy: 0.8828 - val_loss: 0.5897 - val_accuracy: 0.8095
Epoch 15/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.3163 - accuracy: 0.8897 - val_loss: 0.5692 - val_accuracy: 0.8196
Epoch 16/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.2925 - accuracy: 0.8983 - val_loss: 0.5771 - val_accuracy: 0.8213
Epoch 17/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.2770 - accuracy: 0.9027 - val_loss: 0.5220 - val_accuracy: 0.8355
Epoch 18/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.2576 - accuracy: 0.9110 - val_loss: 0.8496 - val_accuracy: 0.7651
Epoch 19/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.2446 - accuracy: 0.9163 - val_loss: 0.5671 - val_accuracy: 0.8309
Epoch 20/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.2308 - accuracy: 0.9202 - val_loss: 0.6052 - val_accuracy: 0.8303
Epoch 21/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.2150 - accuracy: 0.9254 - val_loss: 0.6799 - val_accuracy: 0.8145
Epoch 22/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.2024 - accuracy: 0.9299 - val_loss: 0.6664 - val_accuracy: 0.8232

Epoch 00022: ReduceLROnPlateau reducing learning rate to 0.00020000000949949026.
Epoch 23/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.1253 - accuracy: 0.9566 - val_loss: 0.5437 - val_accuracy: 0.8607
Epoch 24/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.0989 - accuracy: 0.9652 - val_loss: 0.5878 - val_accuracy: 0.8515
Epoch 25/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.0834 - accuracy: 0.9700 - val_loss: 0.5921 - val_accuracy: 0.8587
Epoch 26/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.0784 - accuracy: 0.9725 - val_loss: 0.5951 - val_accuracy: 0.8569
Epoch 27/30
1329/1329 [==============================] - 8s 6ms/step - loss: 0.0718 - accuracy: 0.9741 - val_loss: 0.6518 - val_accuracy: 0.8548

Epoch 00027: ReduceLROnPlateau reducing learning rate to 4.0000001899898055e-05.
Epoch 00027: early stopping

Keras를 항상 급하게 수박 겉핥기식으로만 사용하다가 온라인 강좌를 들으며 실습하던 중 또 까먹을까봐 기록으로 남겨둠

 

데이터는 fasion_mnist(Keras 내장 데이터)

 

1. 검증데이터(테스트데이터) 없이 그냥 학습하기 - 생성된 모델의 evaluate 함수를 이용해서 테스트 데이터로 확인 가능하다

1-1) 데이터 읽어서 정규화(train, test)

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

#데이터를 0~1 값으로 정규화
def get_preprocessed_data(images, labels):
    images = np.array(images/255.0, dtype=np.float32)
    labels = np.array(labels, dtype=np.float32)
    return images, labels

train_images, train_labels = get_preprocessed_data(train_images, train_labels)
test_images, test_labels = get_preprocessed_data(test_images, test_labels)

#print("train dataset shape:", train_images.shape, train_labels.shape)
#print("test dataset shape:", test_images.shape, test_labels.shape)

> 결과

train dataset shape: (60000, 28, 28) (60000,)
test dataset shape: (10000, 28, 28) (10000,)

1-2) 모델 생성

from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Sequential

model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(100, activation='relu'),
    Dense(30, activation='relu'),
    Dense(10, activation='softmax')
])

model.summary()

>결과 : 1차원 배열 만들기

  - flatten -> dense : 784(flatten layer) x 100(dense layer) + 100(bias)

  - dense -> dense_1 : 100 x 30 + 30

  - dense_1 -> dense_2 : 30 x 10 + 10

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense (Dense)                (None, 100)               78500     
_________________________________________________________________
dense_1 (Dense)              (None, 30)                3030      
_________________________________________________________________
dense_2 (Dense)              (None, 10)                310       
=================================================================
Total params: 81,840
Trainable params: 81,840
Non-trainable params: 0

1-3) 모델 컴파일(전에 어디서 들었을 때는 생성된 노드들 조립하는 과정이라고 들었는데 맞는지는...????)

  - 분류 모델이므로 categorical_crossentropy

 

from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import CategoricalCrossentropy
from tensorflow.keras.metrics import Accuracy

model.compile(optimizer=Adam(0.001), loss='categorical_crossentropy', metrics=['accuracy'])

1-4) 레이블을 원핫 인코딩으로 변경

from tensorflow.keras.utils import to_categorical

train_oh_labels = to_categorical(train_labels)
test_oh_labels = to_categorical(test_labels)

print(train_oh_labels.shape, test_oh_labels.shape)

>결과

(60000, 10) (10000, 10)

1-5) 학습하기

history = model.fit(x=train_images, y=train_oh_labels, batch_size=32, epochs=20, verbose=1)

> 결과

  - 데이터는 60000개, batch_size가 32개 이므로 데이터 전체를 1번 돌면 1875(60000/32)번에 나눠서 학습하게 된다

  - epoch가 20 이므로 20번을 학습

  - 그런데, loss와 accuracy는 학습데이터에서 추출하여 측정했으므로 학습데이터와 중복되어 overfitting 여부 확인 어려움

Epoch 1/20
1875/1875 [==============================] - 7s 3ms/step - loss: 0.5217 - accuracy: 0.8173
Epoch 2/20
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3848 - accuracy: 0.8622
Epoch 3/20
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3449 - accuracy: 0.8744
...............................................................
...............................................................
...............................................................
1875/1875 [==============================] - 5s 3ms/step - loss: 0.2013 - accuracy: 0.9238
Epoch 19/20
1875/1875 [==============================] - 5s 3ms/step - loss: 0.1993 - accuracy: 0.9246
Epoch 20/20
1875/1875 [==============================] - 5s 3ms/step - loss: 0.1924 - accuracy: 0.9275

1-6) loss와 accuracy 이력 보기

print(history.history['loss'])
print(history.history['accuracy'])

> 결과

[0.5217015743255615, 0.3847706913948059, 0.3449043929576874, 0.32317453622817993, 0.30555015802383423, 0.2914452850818634, 0.2787344753742218, 0.2689889371395111, 0.2602957487106323, 0.24981915950775146, 0.2438966929912567, 0.23561082780361176, 0.22739382088184357, 0.22206111252307892, 0.2169351726770401, 0.2127165049314499, 0.20825491845607758, 0.20133952796459198, 0.1992630511522293, 0.1923792064189911]
[0.8172666430473328, 0.8622000217437744, 0.8743500113487244, 0.8816999793052673, 0.8874333500862122, 0.8915833234786987, 0.8980833292007446, 0.8997166752815247, 0.9035999774932861, 0.9058499932289124, 0.9086833596229553, 0.9110833406448364, 0.9141833186149597, 0.9162166714668274, 0.9170833230018616, 0.9195833206176758, 0.9207833409309387, 0.9238499999046326, 0.9246166944503784, 0.9274666905403137]

1-7) 생성된 모델로 예측하기(2개이상의 이미지 & 1개의 이미지 예측)

#2개 이상의 이미지 입력시
pred_proba_multi = model.predict(test_images)
#특정 index의 예측값을 원핫인코딩->숫자값으로 변경
pred_multi = np.argmax(pred_proba_multi[0])

#1개의 이미지만 입력시에는 차원을 늘려서 입력해야 한다
pred_proba_single = model.predict(np.expand_dims(test_images[0], axis=0))
#다시 차원을 줄인다
pred_single = np.argmax(np.squeeze(pred_proba_single))
#pred_single = np.argmax(pred_proba_single[0])으로 해도 되지 않을까

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
print('target class value:', test_labels[0], 'predicted class value:', pred_single)

> 결과

target class value: 9.0 predicted class value: 9

1-8) 테스트 데이터 셋으로 모델 검증하기

model.evaluate(test_images, test_oh_labels, batch_size=64)

>결과

157/157 [==============================] - 0s 2ms/step - loss: 0.3832 - accuracy: 0.8859
[0.38321688771247864, 0.8859000205993652]

 

 

2. 학습시 테스트 데이터의 accuracy와 loss 함께 확인하기

  - 기본적으로 학습이 시작되면(model.fit) 중간에 하이퍼파라미터를 변경할 수 없지만, callback을 통하여 변경가능함

2-1) 데이터를 불러와서 train 데이터를 나눔(train data -> train data & val data)

  - 데이터를 나눌 때 sklearn 이용함

  - 아래 코드에서는 15%의 데이터를 validation 데이터로 분리함

import numpy as np 
import pandas as pd
from tensorflow.keras.datasets import fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

def get_preprocessed_data(images, labels):
    images = np.array(images/255.0, dtype=np.float32)
    labels = np.array(labels, dtype=np.float32)
    
    return images, labels
    
train_images, train_labels = get_preprocessed_data(train_images, train_labels)
test_images, test_labels = get_preprocessed_data(test_images, test_labels)


from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical

# 기존 학습 데이터를 다시 학습과 검증 데이터 세트로 분리
tr_images, val_images, tr_labels, val_labels = train_test_split(train_images, train_labels, test_size=0.15, random_state=2021)
print('train과 validation shape:', tr_images.shape, tr_labels.shape, val_images.shape, val_labels.shape)

# OHE 적용
tr_oh_labels = to_categorical(tr_labels)
val_oh_labels = to_categorical(val_labels)

print('after OHE:', tr_oh_labels.shape, val_oh_labels.shape)

> 결과 (51000개의 train, 9000개의  val 데이터로 분리)

print('after OHE:', tr_oh_labels.shape, val_oh_labels.shape)
train과 validation shape: (51000, 28, 28) (51000,) (9000, 28, 28) (9000,)
after OHE: (51000, 10) (9000, 10)

2-2) 모델 생성은 같으나 학습시 validation 데이터를 레이블과 함께 tuple로 입력

from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam

INPUT_SIZE = 28
model = Sequential([
    Flatten(input_shape=(INPUT_SIZE, INPUT_SIZE)),
    Dense(100, activation='relu'),
    Dense(30, activation='relu'),
    Dense(10, activation='softmax')
])

model.compile(optimizer=Adam(0.001), loss='categorical_crossentropy', metrics=['accuracy'])


#validation data는 tuple 로 넣어준다
history = model.fit(x=tr_images, y=tr_oh_labels, batch_size=128, validation_data=(val_images, val_oh_labels), 
                    epochs=20, verbose=1)

> 결과 : val_loss, val_accuracy를 추가로 확인 가능하다

Epoch 1/20
399/399 [==============================] - 3s 4ms/step - loss: 0.6180 - accuracy: 0.7885 - val_loss: 0.4737 - val_accuracy: 0.8350
Epoch 2/20
399/399 [==============================] - 1s 3ms/step - loss: 0.4172 - accuracy: 0.8536 - val_loss: 0.4027 - val_accuracy: 0.8574
Epoch 3/20
399/399 [==============================] - 1s 3ms/step - loss: 0.3766 - accuracy: 0.8664 - val_loss: 0.3831 - val_accuracy: 0.8631
...................................................................
...................................................................
Epoch 18/20
399/399 [==============================] - 1s 3ms/step - loss: 0.2224 - accuracy: 0.9169 - val_loss: 0.3041 - val_accuracy: 0.8912
Epoch 19/20
399/399 [==============================] - 1s 3ms/step - loss: 0.2119 - accuracy: 0.9205 - val_loss: 0.3205 - val_accuracy: 0.8850
Epoch 20/20
399/399 [==============================] - 1s 3ms/step - loss: 0.2094 - accuracy: 0.9217 - val_loss: 0.3190 - val_accuracy: 0.8901

3. 그래프로도 확인 가능하다

import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.plot(history.history['loss'], label='loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.legend()

> 결과

<matplotlib.legend.Legend at 0x7f00188b7e10>

 

MNIST에 Convolution, Relu, MaxPool, Dropout을 추가해서 Accuracy가 99%까지 올라감

 

import tensorflow.compat.v1 as tf
import numpy as np

mnist = tf.keras.datasets.mnist

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape([-1, 784])
test_images  = test_images.reshape([-1,784])
train_images = train_images / 255.
test_images  = test_images / 255.
print(train_images[0])
#train_labels = train_labels.reshape([-1, 784])
print('train_images.shape : ', train_images.shape)
#from tensorflow.examples.tutorials.mnist import input_data

tf.disable_v2_behavior()

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


nb_classes = 10


X = tf.placeholder(tf.float32, [None, 784])
X_img = tf.reshape(X, [-1, 28, 28, 1])
Y = tf.placeholder(tf.float32, [None, nb_classes])

keep_prob = tf.placeholder(tf.float32)

# 3x3 filter, color=1(Gray), # of Filter : 32
W1 = tf.Variable(tf.random_normal([3,3,1,32], stddev=0.01))
L1 = tf.nn.conv2d(X_img, W1, strides=[1,1,1,1], padding='SAME')
# => 입력의 이미지 크기와 같다 'SAME'이므로
# => 출력 이미지는 32개가 된다.

L1 = tf.nn.relu(L1)

#kernel size = 2 x 2
#stride : 2 x 2
L1 = tf.nn.max_pool(L1, ksize=[1,2,2,1],
                    strides=[1,2,2,1], padding='SAME')
L1 = tf.nn.dropout(L1, keep_prob=keep_prob)
#stride가 2x2이므로 28x28 이미지가 14x14가 된다.
'''
Tensor("Conv2D:0", shape=(?,28,28,32), dtype=float32)
Tensor("Relu:0", shape=(?,28,28,32), dtype=float32)
Tensor("MaxPool:0", shape=(?,14,14,32), dtype=float32)
'''
#첫번째 Convolution Layer의 출력 : 14(가로)x14(세로)x32(이미지 갯수 = 필터 수)


#input : (?,14,14, 32) #Filter : 64
W2 = tf.Variable(tf.random_normal([3,3,32, 64], stddev=0.01))
L2 = tf.nn.conv2d(L1, W2, strides=[1,1,1,1], padding='SAME')
#Conv : (?,14,14,64)
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
#Pool : (?,7,7,64)
#Pool : (?,7,7,64)
#L2 = tf.reshape(L2, [-1, 7 * 7 * 64])
L2 = tf.nn.dropout(L2, keep_prob=keep_prob)

'''
Tensor("Conv2D_1:0", shape=(?,14,14,64), dtype=float32)
Tensor("Relu_1:0", shape=(?,14,14,64), dtype=float32)
Tensor("MaxPool_1:0", shape=(?,7,7,64), dtype=float32)
Tensor("Reshape_1:0", shape=(?,3136), dtype=float32)
'''

W3 = tf.Variable(tf.random_normal([3,3,64, 128], stddev=0.01))
L3 = tf.nn.conv2d(L2, W3, strides=[1,1,1,1], padding='SAME')
#Conv : (?,14,14,64)
L3 = tf.nn.relu(L3)
L3 = tf.nn.max_pool(L3, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
#Pool : (?,7,7,64)
#Pool : (?,7,7,64)
L3 = tf.nn.dropout(L3, keep_prob=keep_prob)
L3 = tf.reshape(L3, [-1, 128 * 4 * 4])

W4 = tf.get_variable("W4", shape=[128*4*4, 625])
b4 = tf.Variable(tf.random_normal([625]))
L4 = tf.nn.relu(tf.matmul(L3, W4) + b4)
L4 = tf.nn.dropout(L4, keep_prob=keep_prob)

W5 = tf.get_variable("W5", shape=[625, 10])
b5 = tf.Variable(tf.random_normal([10]))

#batch_xs, batch_ys = mnist.train.next_batch(100)

hypothesis = tf.nn.softmax(tf.matmul(L4, W5) + b5)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=hypothesis, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cost)

#cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis = 1))
#optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.05).minimize(cost)

prediction = tf.argmax(hypothesis, 1) #예측한 결과를 0~6사이의 값으로 만든다
is_correct = tf.equal(prediction, tf.argmax(Y, 1))#예측한 결과와 Y 데이터를 비교
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) #이것들을 평균낸다

training_epochs = 100
batch_size = 100

import matplotlib.pyplot as plt
import random

#'''
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(training_epochs):
        avg_cost = 0
        total_batch = int(train_images.shape[0] / batch_size)

        for i in range(total_batch):
            s_idx = int(train_images.shape[0] * i / total_batch)
            e_idx = int(train_images.shape[0] * (i+1)/ total_batch)
            #print('s_idx : ', s_idx)
            #print('width : ', width)
            batch_xs = train_images[s_idx : e_idx]
            batch_ys = train_labels[s_idx : e_idx]
            #print('batch_xs.shape : ', batch_xs.shape)
            #print('batch_ys.shape : ', batch_ys.shape)
            #Y_one_hot = tf.one_hot(batch_ys, nb_classes)
            Y_one_hot = np.eye(nb_classes)[batch_ys]
            #print('Y_one_hot.shape :', Y_one_hot.shape)
            _,c = sess.run([optimizer, cost], feed_dict={X:batch_xs, Y:Y_one_hot, keep_prob:0.5})
            #print('total_batch : ', total_batch, ', c:', c)
            avg_cost += c / total_batch

        print('Epoch:', '%04d' % (epoch+1), 'cost=', '{:.9f}'.format(avg_cost))
    Y_one_hot = np.eye(nb_classes)[test_labels]
    print("Accuracy : ", accuracy.eval(session=sess, feed_dict={X:test_images, Y:Y_one_hot, keep_prob:1}))
    
    r = random.randint(0, test_images.shape[0] - 1)
    print('label : ', test_labels[r:r+1])
    print('Prediction : ', sess.run(tf.argmax(hypothesis, 1), feed_dict={X:test_images[r:r+1], keep_prob:1}))
    plt.imshow(test_images[r:r+1].reshape(28,28), cmap='Greys', interpolation='nearest')
    plt.show()
#'''

2년전인가 수박 겉핡기식으로 겨우겨우 한번 본 영상

 

다 보고 나서 근거 없는 자신감으로 github를 찾아 방황했는데...

 

최근 처음부터 다시 보게 된 강의

 

근데 tensorflow가 2.x로 바뀌면서 강의의 소스가 안 실행이 안되서

(github 소스는 될라나? 확안 해봤음)

 

그래서 나름대로 돌아가게끔 만든 소스를 기록할려고 글을 남김

 

import tensorflow.compat.v1 as tf
import numpy as np

mnist = tf.keras.datasets.mnist

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape([-1, 784])
test_images  = test_images.reshape([-1,784])
train_images = train_images / 255.
test_images  = test_images / 255.
print(train_images[0])
#train_labels = train_labels.reshape([-1, 784])
print('train_images.shape : ', train_images.shape)
#from tensorflow.examples.tutorials.mnist import input_data

tf.disable_v2_behavior()

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



nb_classes = 10

X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, nb_classes])

W = tf.Variable(tf.random_normal([784, nb_classes]))
b = tf.Variable(tf.random_normal([nb_classes]))

#batch_xs, batch_ys = mnist.train.next_batch(100)

hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis = 1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.05).minimize(cost)

prediction = tf.argmax(hypothesis, 1) #예측한 결과를 0~6사이의 값으로 만든다
is_correct = tf.equal(prediction, tf.argmax(Y, 1))#예측한 결과와 Y 데이터를 비교
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) #이것들을 평균낸다

training_epochs = 15
batch_size = 100

import matplotlib.pyplot as plt
import random

#'''
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(training_epochs):
        avg_cost = 0
        total_batch = int(train_images.shape[0] / batch_size)

        for i in range(total_batch):
            s_idx = int(train_images.shape[0] * i / total_batch)
            e_idx = int(train_images.shape[0] * (i+1)/ total_batch)
            #print('s_idx : ', s_idx)
            #print('width : ', width)
            batch_xs = train_images[s_idx : e_idx]
            batch_ys = train_labels[s_idx : e_idx]
            #print('batch_xs.shape : ', batch_xs.shape)
            #print('batch_ys.shape : ', batch_ys.shape)
            #Y_one_hot = tf.one_hot(batch_ys, nb_classes)
            Y_one_hot = np.eye(nb_classes)[batch_ys]
            #print('Y_one_hot.shape :', Y_one_hot.shape)
            _,c = sess.run([optimizer, cost], feed_dict={X:batch_xs, Y:Y_one_hot})
            #print('total_batch : ', total_batch, ', c:', c)
            avg_cost += c / total_batch

        print('Epoch:', '%04d' % (epoch+1), 'cost=', '{:.9f}'.format(avg_cost))
    Y_one_hot = np.eye(nb_classes)[test_labels]
    print("Accuracy : ", accuracy.eval(session=sess, feed_dict={X:test_images, Y:Y_one_hot}))
    
    r = random.randint(0, test_images.shape[0] - 1)
    print('label : ', test_labels[r:r+1])
    print('Prediction : ', sess.run(tf.argmax(hypothesis, 1), feed_dict={X:test_images[r:r+1]}))
    plt.imshow(test_images[r:r+1].reshape(28,28), cmap='Greys', interpolation='nearest')
    plt.show()
#'''

'딥러닝' 카테고리의 다른 글

[KERAS] 학습시 검증데이터 세트 이용 유무  (0) 2021.10.29
다시 실습해 보는 모두의 딥러닝(2) - Lab11(Mnist)  (0) 2021.04.02
Octave 실습정리 4  (0) 2021.03.16
Octave 실습 정리 2  (0) 2021.03.15
Octave 실습 정리 1  (0) 2021.03.15

 

 

 

* Sin 곡선 그리기

 

* Cos 곡선 그리기

* 2개 이상의 그래프 그리기

* 그래프 저장하려면

cd 'd:\test\';print -dpng 'myPlot.png'

 

* 그래프를 닫으려면

close

* 2개 이상의 그래프를 그리려면

 

* 하나의 윈도우에 두개 이상의 그래프(SubPlot)

* clf(clear fiture) - 화면 지우기

 

 

 

 

 

* 행렬의 크기 및 길이(length)

  - length : 가장 높은 차원의 길이만 구하는 듯

 

* 현재 디렉토리(pwd)

#who 명령 : 메모리 상의 변수
#load 함수 : 데이터 파일을 읽어들임, 변수명은 파일명으로
load('featureX.dat') # featureX 변수에 할당됨
#whos 명령 : 메모리 상의 변수와 상세 정보등을 보여줌(Name, Size, Bytes, Class)
#clear featureX : 메모리상에서 featureX 변수 제거

V = featuresX(1:10) #features 처음 10개의 데이터를 V에 저장
save hello.mat V; #V 변수의 데이터를 hello.mat파일에 저장

clear #모든 변수를 다 지움

load hello.mat #V 변수가 다시 생김

save hello.txt V -ascii #텍스트로 저장



 

 

 

 

 

 

'딥러닝' 카테고리의 다른 글

다시 실습해보는 모두의 딥러닝(1) - Lab7(MNIST)  (0) 2021.04.02
Octave 실습정리 4  (0) 2021.03.16
Octave 실습 정리 1  (0) 2021.03.15
Mnist 학습 후 c#으로 실행하기  (0) 2020.03.10
KERAS - MNIST 소스 분석  (0) 2020.03.06

+ Recent posts