So I implemented k-fold from this notebook by using the new fastai version and for a vision problem (the notebook is for tabular). So am I doing it ok or are there some things that need to be changed?
Here is my code:
train_df, test_df = train_test_split(full_df, test_size=0.1)
folds = 4
skf = StratifiedKFold(n_splits=folds, shuffle=True)
val_pct = []
test_pct = []
batch_size = 32
for train_index, val_index in skf.split(train_df.index, train_df['label']):
train_block = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_x=get_x,
get_y=get_y,
splitter=IndexSplitter(val_index), # added val_index
item_tfms=[
Resize(384),
FlipItem(p=0.4),
RandomCrop(300)
],
batch_tfms=[Normalize.from_stats(*imagenet_stats)]
)
test_block = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_x=get_x,
get_y=get_y,
item_tfms=[
Resize(384)
],
batch_tfms=[Normalize.from_stats(*imagenet_stats)]
)
train_dl = train_block.dataloaders(train_df, bs=batch_size)
test_dl = test_block.dataloaders(test_df, bs=batch_size)
test_dl.valid = test_dl.train
# train model
learn = train(train_dl, resnet101, epochs=10, freeze_epochs=7)
_, val = learn.validate()
learn.dls.valid = test_dl.valid
_, test = learn.validate()
print('done, appending results.. \n')
val_pct.append(val)
test_pct.append(test)
Also, should the augmentations(item_tfms, batch_tfms) from the train_block be used for the test_block.
Thanks a lot!