BK101
BK101

Reputation: 141

Not able to predict output in fastai

Noob here.

Here's the dataset that I'm working on https://www.kaggle.com/arpitjain007/game-of-deep-learning-ship-datasets

and I'm using fastai, I've successfully built the model but I have no idea how to test it with 'test.csv' file.

Here's my code

from fastai import *
from fastai.vision import *

path = '../input/train'
path = Path(path)
path.ls()
df = pd.read_csv(path/'train.csv')
data = ImageDataBunch.from_df('../input/train/images', df, ds_tfms=get_transforms(), size=224, bs=64 ).normalize(imagenet_stats)
learn = cnn_learner(data, models.resnet50, metrics=accuracy,  model_dir='/kaggle/working/models')
learn.fit_one_cycle(5)
df_test = pd.read_csv('../input/test_ApKoW4T.csv')

I don't know how to use the Test Dataframe to predict.

Upvotes: 3

Views: 4342

Answers (3)

prosti
prosti

Reputation: 46341

The trick is to use the ImageList instead ImageDataBunch.

Upvotes: 0

BK101
BK101

Reputation: 141

All i had to do was create an Image List

train = ImageList.from_df(df,'../input/train/images')
test = ImageList.from_df(df_test, '../input/train/images')

then create ImageDataBunch

data = ImageDataBunch.from_df('../input/train/images', df, 
ds_tfms=get_transforms(), size=224, bs=64 ).normalize(imagenet_stats)

then add test

data.add_test(test)

and then predict using

predictions, *_ = learn.get_preds(DatasetType.Test)
labels = np.argmax(predictions, 1)
df_test['category'] = labels

Upvotes: 9

Tree
Tree

Reputation: 31361

Check out this kernel https://www.kaggle.com/matejthetree/digit-recognizer-fast-ai-customimagelist?scriptVersionId=14597759

when initializing data you add test bunch to it

data = (CustomImageList.from_csv_custom(path=path, csv_name='train.csv', imgIdx=1)
                .split_by_rand_pct(.2)
                .label_from_df(cols='label')
                .add_test(test, label=0)
                .transform(tfms)
                .databunch(bs=128, num_workers=0)
                .normalize(imagenet_stats))

later you get predictions

predictions, *_ = learn.get_preds(DatasetType.Test)
labels = np.argmax(predictions, 1)
# output to a file
submission_df = pd.DataFrame({'ImageId': list(range(1,len(labels)+1)), 'Label': labels})
submission_df.to_csv(f'submission.csv', index=False)

Upvotes: 1

Related Questions