해결된 질문
작성
·
348
0
keras-yolo3로 학습을 하려고 했는데 도저히 에러 발생에서 해결을 못하겠습니다....
주피터 노트북에서 실행했습니다
"""
Retrain the YOLO model for your own dataset.
"""
import os
from pathlib import Path
HOME_DIR = 'C:/test/'
ANNO_DIR = 'C:/test/label_xml/'
#xml 파일들
IMAGE_DIR = 'c:/test/images1/'
#이미지 파일들
print(ANNO_DIR)
print(IMAGE_DIR)
files = os.listdir(ANNO_DIR)
files2 = os.listdir(IMAGE_DIR)
print('파일 개수는:',len(files))
print('파일 개수는:',len(files2))
import numpy as np
import tensorflow.keras.backend as K
from keras.layers import Input, Lambda
from keras.models import *
from keras.optimizers import Adam
from keras.callbacks import TensorBoard, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from tensorflow.python.ops import control_flow_ops
import sys
LOCAL_PACKAGE_DIR = os.path.abspath("c:/test/SED")
sys.path.append(LOCAL_PACKAGE_DIR)
from yolo3.model import preprocess_true_boxes, yolo_body, tiny_yolo_body, yolo_loss
from yolo3.utils import get_random_data
from train import get_classes, get_anchors
from train import create_model, data_generator, data_generator_wrapper
annotation_path = 'c:/test/label_xml/annotation.csv'
log_dir = 'c:/test/logs/000/'
classes_path = 'c:/test/model_data/voc_classes.txt'
anchors_path = 'c:/test/model_data/yolo_anchors.txt'
class_names = get_classes(classes_path)
num_classes = len(class_names)
anchors = get_anchors(anchors_path)
model_weights_path = 'c:/test/model_data/yolo.h5'
input_shape = (416,416)
is_tiny_version = len(anchors)==6 # default setting
# create_tiny_model(), create_model() 함수의 인자 설정을 원본 train.py에서 수정.
if is_tiny_version:
model = create_tiny_model(input_shape, anchors, num_classes,
freeze_body=2, weights_path=model_weights_path)
else:
# create_model 은 해당 패키지의 tarin.py 내부에 있는 클래스를 사용했다. 이 함수는 keras 모듈이 많이 사용한다. 우선 모르는 건 pass하고 넘어가자.
model = create_model(input_shape, anchors, num_classes,
freeze_body=2, weights_path=model_weights_path) # make sure you know what you freeze
# epoch 마다 call back 하여 모델 파일 저장.
# 이것 또한 Keras에서 많이 사용하는 checkpoint 저장 방식인듯 하다. 우선 이것도 모르지만 넘어가자.
logging = TensorBoard(log_dir=log_dir)
checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5',
monitor='val_loss', save_weights_only=True, save_best_only=True, period=3)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1)
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)
val_split = 0.1 # train data : val_data = 9 : 1
with open(annotation_path) as f:
# 이러니 annotation 파일이 txt이든 csv이든 상관없었다.
lines = f.readlines()
# 랜덤 시드 생성 및 lines 셔플하기
np.random.seed(10101)
np.random.shuffle(lines)
np.random.seed(None)
# 데이터셋 나누기
num_val = int(len(lines)*val_split)
num_train = len(lines) - num_val
# 여기서 부터 진짜 학습 시작!
# create_model() 로 반환된 yolo모델에서 trainable=False로 되어 있는 layer들 제외하고 학습
if True:
# optimizer와 loss 함수 정의
# 위에서 사용한 create_model 클래스의 맴버함수를 사용한다.
model.compile(optimizer=Adam(lr=1e-3), loss={
# use custom yolo_loss Lambda layer.
'yolo_loss': lambda y_true, y_pred: y_pred})
batch_size = 4
print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))
# foward -> backpropagation -> weight 갱신 -> weight 저장
# checkpoint 만드는 것은 뭔지 모르겠으니 pass...
model.fit_generator(
data_generator_wrapper(lines[:num_train], batch_size, input_shape, anchors, num_classes),
steps_per_epoch=max(1, num_train//batch_size),
validation_data=data_generator_wrapper(lines[num_train:], batch_size, input_shape, anchors, num_classes),
validation_steps=max(1, num_val//batch_size),
epochs=50,
initial_epoch=0,
callbacks=[logging, checkpoint])
model.save_weights(log_dir + 'trained_weights_stage_1.h5')
# create_model() 로 반환된 yolo모델에서 trainable=False로 되어 있는 layer들 없이, 모두 True로 만들고 다시 학습
if True:
for i in range(len(model.layers)):
model.layers[i].trainable = True
model.compile(optimizer=Adam(lr=1e-4), loss={'yolo_loss': lambda y_true, y_pred: y_pred}) # recompile to apply the change
print('Unfreeze all of the layers.')
batch_size = 4 # note that more GPU memory is required after unfreezing the body
print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))
model.fit_generator(
data_generator_wrapper(lines[:num_train], batch_size, input_shape, anchors, num_classes),
steps_per_epoch=max(1, num_train//batch_size),
validation_data=data_generator_wrapper(lines[num_train:], batch_size, input_shape, anchors, num_classes),
validation_steps=max(1, num_val//batch_size),
epochs=100,
initial_epoch=50,
callbacks=[logging, checkpoint, reduce_lr, early_stopping])
model.save_weights(log_dir + 'trained_weights_final.h5')
이렇게 해주고 실행을 했습니다. 이미지 경로도 제대로 설정해줬는데 파일이나 폴더를 찾을 수가 없다고 하는데 어떻게 해주어야 할까요....
저 images1 폴더를 다른 이름으로 수정하거나 없애도 경로가 계속 'c:/test/images1\\hardhatvest' 이곳을 가리킵니다.
Train on 29 samples, val on 3 samples, with batch size 4.
Epoch 1/50
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_20040\2424985784.py in <module>
20 epochs=50,
21 initial_epoch=0,
---> 22 callbacks=[logging, checkpoint])
23 model.save_weights(log_dir + 'trained_weights_stage_1.h5')
24
~\anaconda3\envs\test\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~\anaconda3\envs\test\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1730 use_multiprocessing=use_multiprocessing,
1731 shuffle=shuffle,
-> 1732 initial_epoch=initial_epoch)
1733
1734 @interfaces.legacy_generator_methods_support
~\anaconda3\envs\test\lib\site-packages\keras\engine\training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
183 batch_index = 0
184 while steps_done < steps_per_epoch:
--> 185 generator_output = next(output_generator)
186
187 if not hasattr(generator_output, '__len__'):
~\anaconda3\envs\test\lib\site-packages\keras\utils\data_utils.py in get(self)
740 "`use_multiprocessing=False, workers > 1`."
741 "For more information see issue #1638.")
--> 742 six.reraise(*sys.exc_info())
~\anaconda3\envs\test\lib\site-packages\six.py in reraise(tp, value, tb)
717 if value.__traceback__ is not tb:
718 raise value.with_traceback(tb)
--> 719 raise value
720 finally:
721 value = None
~\anaconda3\envs\test\lib\site-packages\keras\utils\data_utils.py in get(self)
709 try:
710 future = self.queue.get(block=True)
--> 711 inputs = future.get(timeout=30)
712 self.queue.task_done()
713 except mp.TimeoutError:
~\anaconda3\envs\test\lib\multiprocessing\pool.py in get(self, timeout)
655 return self._value
656 else:
--> 657 raise self._value
658
659 def _set(self, i, obj):
~\anaconda3\envs\test\lib\multiprocessing\pool.py in worker(inqueue, outqueue, initializer, initargs, maxtasks, wrap_exception)
119 job, i, func, args, kwds = task
120 try:
--> 121 result = (True, func(*args, **kwds))
122 except Exception as e:
123 if wrap_exception and func is not _helper_reraises_exception:
~\anaconda3\envs\test\lib\site-packages\keras\utils\data_utils.py in next_sample(uid)
648 The next value of generator `uid`.
649 """
--> 650 return six.next(_SHARED_SEQUENCES[uid])
651
652
c:\test\train.py in data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes)
177 if i==0:
178 np.random.shuffle(annotation_lines)
--> 179 image, box = get_random_data(annotation_lines[i], input_shape, random=True)
180 image_data.append(image)
181 box_data.append(box)
c:\test\yolo3\utils.py in get_random_data(annotation_line, input_shape, random, max_boxes, jitter, hue, sat, val, proc_img)
37 '''random preprocessing for real-time data augmentation'''
38 line = annotation_line.split()
---> 39 image = Image.open(line[0])
40 iw, ih = image.size
41 h, w = input_shape
~\anaconda3\envs\test\lib\site-packages\PIL\Image.py in open(fp, mode, formats)
3090
3091 if filename:
-> 3092 fp = builtins.open(filename, "rb")
3093 exclusive_fp = True
3094
FileNotFoundError: [Errno 2] No such file or directory: 'c:/test/images1\\hardhatvest'
해결했습니다. 제가 xml 파일들을 csv 파일로 변환했었는데 csv 파일에 이미지 경로가 잘못 되었었네요.
답변해주셔서 감사합니다. 순간적으로 답변 보고 떠올랐네요.