!pip install git+https://github.com/qubvel/segmentation_models.pytorch
import segmentation_modesl_pytorch as smp
model = smp.Unet(
encoder_name = 'efficientnet-b0",
encoder_weights = 'imagenet', # use 'imagenet' pre-trained weights for encoder initialization
in_channels = 3, # model input channels (1 for gray-scale images, 3 for RGB)
classes=10) # model output channels (number of classes in your dataset)
이외에도 smp.UnetPlusPlus, smp.Manet, smp.Linknet, smp.FPN, smp.PSPNet, smp.PAN, smp.DeepLabV3, smp.DeepLabV3Plus 의 9가지 모델을 이용할 수 있다.
실험을 시작하기 전, 실험 환경이 잘 설정되었는지 확인하자.
샘플링을 통해 데이터 셋의 일부분만 추출하고, epoch을 1~2 정도로 작게 설정하여 loss가 감소하는지 확인한다.
if CFG.debug:
CFG.epochs=2
train = train.sample(frac=0.05, random_state=CFG.seed).reset_index(drop=True)
모델의 성능을 비교할 때, 실험마다 성능이 달라지는 것을 방지하고 재생산하기 위해 seed를 고정해야 한다.
def set_seeeds(seed=42):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
set_seeds()
또는 validation set의 seed 고정은 아래와 같이 할 수 있다.
from sklearn.model_selection import train_test_split
df_train, df_valid = train_test_split(label, test_size=0.2, random_state=42)