无法预测Fastai中的输出

时间:2019-05-27 13:39:52

标签: python machine-learning deep-learning conv-neural-network fast-ai

Noob在这里。

这是我正在https://www.kaggle.com/arpitjain007/game-of-deep-learning-ship-datasets上工作的数据集

我正在使用fastai,我已经成功构建了模型,但是我不知道如何使用'test.csv'文件进行测试。

这是我的代码

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')

我不知道如何使用测试数据框进行预测。

3 个答案:

答案 0 :(得分:1)

我要做的就是创建一个图像列表

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

然后创建ImageDataBunch

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

然后添加测试

data.add_test(test)

然后预测使用

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

答案 1 :(得分:0)

签出此内核 https://www.kaggle.com/matejthetree/digit-recognizer-fast-ai-customimagelist?scriptVersionId=14597759

初始化数据时,您向其中添加测试束

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))

稍后您会得到预测

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)

答案 2 :(得分:0)

诀窍是使用ImageList代替ImageDataBunch