728x90
tf.keras.utils.image _dataset_from_directory 명령을 사용하여 이미지 데이터 세트를 종류별로 분류하는 학습 과정에서 callback 파라메터를 설정하여 내부적으로 생성되는 가중치를 특정 폴더 주소인 checkpoint 주소를 지정하여 저장하자.
차 후에 별도의 테스트 데이터를 준비한 후에 가중치 생성 시에 사용했던 모델을 불러 낸 후 저장된 가중치를 업로딩하여 학습과정 없이 평가(evaluation) 작업을 실행해 보자.
첫째로 가중치를 생성했던 모델을 그대로 부른다.
둘째로 checkpoint 경로를 지정하자. training_1 은 폴더명이며 현재의 실행파일이 위치한 폴더에 설치된다.
셋째로 tf.keras.callbacks.ModelCheckpoint 명령을 사용하여 callback 변수를 설정한다.
넷째로 model.fit 의 callbackㄴ 파라메터를 설정한다.
model = create_model(num_classes) checkpoint_path = "training_1/cp.ckpt" checkpoint_dir = os.path.dirname(checkpoint_path) print('dir name : ', checkpoint_dir) cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path, save_weights_only=True, verbose=1) history = model.fit( train_ds, validation_data=val_ds, epochs=20, callbacks=[cp_callback] ) |
callbacks 기능에 의해 가중치가 지정된 경로에 저장된 후에 model.fit 명령이 제외된 상태에서 가중치를 업로드하여 아래와 같이 코드를 실행해 보자.
checkpoint_path = "training_1/cp.ckpt" # Create a basic model instance model = create_model(num_classes) # Loads the weights model.load_weights(checkpoint_path) loss, acc = model.evaluate(valid_image_batch, valid_label_batch, verbose=2) # 검증데이터 사용 70% 선 확인 print("Untrained model, accuracy: {:5.2f}%".format(100 * acc)) loss, acc = model.evaluate(train_image_batch, train_label_batch, verbose=2) # 학습데이터 사용 print("Already trained model, accuracy: {:5.2f}%".format(100 * acc)) # 100% 확인 |
아래의 출력 결과를 살펴보자.
checkpoint_path를 확인 후 첨부된 파일을 다운받아 실행해보자.
#callbackexecution.py
'인공지능 응용 공학' 카테고리의 다른 글
Prediction of unseen image using callback weights (0) | 2023.06.11 |
---|---|
OpenCV 명령을 응용한 하네스 커넥터 확대 이미지의 의 윤곽선 추출 (0) | 2023.06.07 |
tf.keras.preprocessing.image.ImageDataGenerator 에서 tf.keras.utils.image _dataset_from_directory 로 변경하여 이미지 분류 (0) | 2023.06.07 |
학습가중치 저장 및 업로딩에 의한 Keras MNIST 수기문자 판독 (0) | 2023.06.03 |
지적재산기반 인공지능응용 공학 목차 PDF 파일 (0) | 2023.03.05 |