• 카테고리

    질문 & 답변
  • 세부 분야

    컴퓨터 비전

  • 해결 여부

    미해결

예측

22.09.05 23:14 작성 조회수 132

0

안녕하세요.

선생님께서 강의를 최대한 쉽게 설명해주시려고 애쓰시는 것 같은데, 제가 부족함이 많아 이해를 못하는 부분이 아직 많습니다.

직접 해보면 좀 이해가 갈까 싶어서 이리저리 적용을 해보고 있는 상황인데요, 막히는 부분이 있어서 질문드립니다!

 

Breed Dataset 에서 저희가 예측할 때는 testdata 라벨이 있었지만,

plant pathology와 같이 Breed dataset에도 test data에 라벨이 없다는 상황이라고 가정한다면..

예측할 때 아래와 같이 label자리(?)에 None이나 labels=None을 쓰면 된다고 생각했는데


test_ds = Breed_Dataset(test_path, None, image_size= ............................)

xception_model.predict(test_ds)


 

단순한 생각이었던 것 같고, 아무리 생각해도 잘 모르겠어서 질문드립니다.

 

감사합니다.

답변 1

답변을 작성해보세요.

0

안녕하십니까,

너무 급하게 생각하지 마시고 차근차근 강의를 좀 더 따라해 보시면 보다 수월해 지실 겁니다.

그런데, 제가 질문을 정확하게 이해는 못했습니다만,

BreedDataset은 labels를 None으로 입력하면 Nonetype 오류가 나게 코드가 작성되었습니다.

만약에 labels를 None으로 입력하고 싶다면 아래와 같이 BreedDataset을 다시 만들어 주시면 됩니다.

바뀐 부분은 __len__()함수에서 self.labels가 아닌 self.image_filenames로 변경해 주는 부분과

__getitem__()의 바로 아래에 label_batch=None으로 초기화 한 부분입니다.

게시판에서 Indent가 잘 안되는 군요. 직접 실습 코드에서 위에서 말씀드린 변경사항을 참조해서 적용해 보시면 될 것 같습니다.

감사합니다.

 

# 입력 인자 image_filenames, labels는 모두 numpy array로 들어옴.

class Breed_Dataset(Sequence):

def init(self, image_filenames, labels, image_size=IMAGE_SIZE, batch_size=BATCH_SIZE,

augmentor=None, shuffle=False, pre_func=None):

'''

파라미터 설명

image_filenames: opencv로 image를 로드할 파일의 절대 경로들

labels: 해당 image의 label들

batch_size: getitem(self, index) 호출 시 마다 가져올 데이터 batch 건수

augmentor: albumentations 객체

shuffle: 학습 데이터의 경우 epoch 종료시마다 데이터를 섞을지 여부

'''

# 객체 생성 인자로 들어온 값을 객체 내부 변수로 할당.

self.image_filenames = image_filenames

self.labels = labels

self.image_size = image_size

self.batch_size = batch_size

self.augmentor = augmentor

self.pre_func = pre_func

# train data의 경우

self.shuffle = shuffle

if self.shuffle:

# 객체 생성시에 한번 데이터를 섞음.

#self.on_epoch_end()

pass

 

# Sequence를 상속받은 Dataset은 batch_size 단위로 입력된 데이터를 처리함.

# len()은 전체 데이터 건수가 주어졌을 때 batch_size단위로 몇번 데이터를 반환하는지 나타남

def len(self):

# batch_size단위로 데이터를 몇번 가져와야하는지 계산하기 위해 전체 데이터 건수를 batch_size로 나누되, 정수로 정확히 나눠지지 않을 경우 1회를 더한다.

return int(np.ceil(len(self.image_filenames) / self.batch_size))

 

# batch_size 단위로 image_array, label_array 데이터를 가져와서 변환한 뒤 다시 반환함

# 인자로 몇번째 batch 인지를 나타내는 index를 입력하면 해당 순서에 해당하는 batch_size 만큼의 데이타를 가공하여 반환

# batch_size 갯수만큼 변환된 image_array와 label_array 반환.

def getitem(self, index):

label_batch = None;

# index는 몇번째 batch인지를 나타냄.

# batch_size만큼 순차적으로 데이터를 가져오려면 array에서 index*self.batch_size:(index+1)*self.batch_size 만큼의 연속 데이터를 가져오면 됨

image_name_batch = self.image_filenames[index*self.batch_size:(index+1)*self.batch_size]

if self.labels is not None:

label_batch = self.labels[index*self.batch_size:(index+1)*self.batch_size]

 

# 만일 객체 생성 인자로 albumentation으로 만든 augmentor가 주어진다면 아래와 같이 augmentor를 이용하여 image 변환

# albumentations은 개별 image만 변환할 수 있으므로 batch_size만큼 할당된 image_name_batch를 한 건씩 iteration하면서 변환 수행.

# image_batch 배열은 float32 로 설정.

image_batch = np.zeros((image_name_batch.shape[0], self.image_size, self.image_size, 3), dtype='float32')

 

# batch_size에 담긴 건수만큼 iteration 하면서 opencv image load -> image augmentation 변환(augmentor가 not None일 경우)-> image_batch에 담음.

for image_index in range(image_name_batch.shape[0]):

image = cv2.cvtColor(cv2.imread(image_name_batch[image_index]), cv2.COLOR_BGR2RGB)

if self.augmentor is not None:

image = self.augmentor(image=image)['image']

#crop 시 잘린 이미지가 원본 이미지와 다르게 되므로 augmentation 적용 후 resize() 적용.

image = cv2.resize(image, (self.image_size, self.image_size))

# 만일 preprocessing_input이 pre_func인자로 들어오면 이를 이용하여 scaling 적용.

if self.pre_func is not None:

image = self.pre_func(image)

 

image_batch[image_index] = image

 

return image_batch, label_batch

 

# epoch가 한번 수행이 완료 될 때마다 모델의 fit()에서 호출됨.

def on_epoch_end(self):

if(self.shuffle):

#print('epoch end')

# 전체 image 파일의 위치와 label를 쌍을 맞춰서 섞어준다. scikt learn의 utils.shuffle에서 해당 기능 제공

self.image_filenames, self.labels = sklearn.utils.shuffle(self.image_filenames, self.labels)

else:

pass

 

test_ds = Breed_Dataset(test_path, None, image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, augmentor=None, shuffle=False, pre_func=xcp_preprocess_input)