from pathlib import Path
from fastai import *
from fastai.vision import *
import torch
import os
!pip install efficientnet-pytorch
from efficientnet_pytorch import EfficientNet
# for dirname, _, filenames in os.walk('/kaggle/input'):
# for filename in filenames:
# print(os.path.join(dirname, filename))
data_folder = Path("/kaggle/input/54-data/54_data")
train_df = pd.read_csv("/kaggle/input/54-data/54_data/train.csv")
test_df = pd.DataFrame({"filename": [f'{i}.jpg' for i in range(1434)]})
test_img = ImageList.from_df(test_df, path=data_folder, folder='test')
trfm = get_transforms(do_flip=True,
max_rotate=20.0,
max_zoom=1.25,
max_lighting=0.2,
max_warp=0.2,
p_affine=0.65,
p_lighting=0.55
)
train_img = (ImageList.from_df(train_df, path=data_folder, folder='train')
.split_by_rand_pct(0.01)
.label_from_df()
.add_test(test_img)
.transform(trfm, size=240)
.databunch(path='.', bs=64, device= torch.device('cuda:0'))
.normalize(imagenet_stats)
)
train_img.show_batch(rows=3, figsize=(7,6))
model = EfficientNet.from_pretrained('efficientnet-b1')
model._fc = nn.Linear(1280, train_img.c)
learn = Learner(train_img, model, metrics=[accuracy])
# learn = cnn_learner(train_img, models.densenet201, metrics=[accuracy])
learn.unfreeze()
learn.lr_find()
learn.recorder.plot(suggestion=True)
# callbacks.EarlyStoppingCallback(learn, min_delta=1e-5, patience=5),
callbacks.SaveModelCallback(learn)
]
learn.callbacks = callbacks
learn.fit_one_cycle(40, 5e-3)
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_top_losses(9, figsize=(7,6))
learn.load('bestmodel')
preds, _ = learn.TTA(ds_type=DatasetType.Test)
test_df['labels'] = np.argmax(preds.numpy(), axis=1)
sub = test_df.copy()
sub['ID'] = test_df.index
sub[['ID', 'labels']].to_csv('submission.csv', index=False, header=False)
sub