본문 바로가기

트러블슈팅

teacher모델의 입력 변형 문제

핵심 질문 리스트

1) teacher 모델의 입력이 (Cx224x224) 일 때, 모델 입력이 Cx64x64로 들어온다면 어떻게 해야할까 ?

2) teacher 모델의 forward 단에서 data transformation 관련 코드를 추가하거나 수정해야할까 ? 

 

그리고 위 질문과 관련된 구체적인 내용은 아래와 같습니다.

teacher 모델의 인풋이 Channelx224(Width)x224(Height) 이고, 전이학습을 위해 teacher 모델에 입력으로 들어오는 이미지 사이즈가 Channelx64(Width)x64(Height)인 경우, 
어떻게 입력되는 데이터의 Width와 Height을 변형시켜서 teacher 모델 입력 사이즈와 입력되는 데이터 사이즈를 어떻게 맞춰주는 것인가? 
특히, 소스코드 상에서 forward_zt() 함수 안에서 입력되는 데이터의 Width와 Height 변형을 일으키는 함수를 사용해야 하는 것일까 ? 
그리고 해결방법으로 DEIT 소스코드를 활용하는게 좋을까? 

 

 

해결방법 정리

 

아래 코드를 활용하면 충분히 변형은 가능할 것으로 보입니다.

 

방법1 

## Design of data transformation

In MMCV, we use various callable data transformation classes to manipulate data. These data transformation classes can accept several configuration parameters for the instantiation and then process the input data dictionary by `__call__` method. All data transformation methods accept a dictionary as the input and produce the output as a dictionary as well. A simple example is as follows:
```python
>>> import numpy as np
>>> from mmcv.transforms import Resize
>>>
>>> transform = Resize(scale=(224, 224))
>>> data_dict = {'img': np.random.rand(256, 256, 3)}
>>> data_dict = transform(data_dict)
>>> print(data_dict['img'].shape)
(224, 224, 3)
```
The data transformation class reads some fields of the input dictionary and may add or update some fields. The keys of these fields are mostly fixed. For example, `Resize` will always read fields such as `"img"` in the input dictionary. More information about the conventions for input and output fields could be found in the documentation of the corresponding class.

 

방법2

mmcv 설치가 번거롭다면, 단순하게 torchvision에 있는 transforms 라이브러리를 활용하는 것도 하나의 방법 입니다.

   import torchvision.transforms as transforms

   transform = transforms.Compose([
       transforms.Resize((224, 224)),  # Width와 Height를 224로 조정
       transforms.ToTensor(),
   ])