text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from albumentations.pytorch.transforms import ToTensorV2

from efficientnet_pytorch import EfficientNet

from efficientnet_pytorch import EfficientNet

from sklearn.preprocessing import MultiLabelBinarizer

from torch.utils.data import Dataset, DataLoader

from transformers import get_cosine_schedule_with_warmup

import cv2

import cv2

import numpy as np

import pandas as pd

import torch

import torch.nn as nn

import torch.optim as optim

df = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')
df['labels'] = df['labels'].apply(lambda string: string.split(' '))
s = list(df['labels'])
mlb = MultiLabelBinarizer()
trainx = pd.DataFrame(mlb.fit_transform(s), columns=mlb.classes_, index=df.index)
trainx.insert(0, 'image', df['image'], True)
trainx

t_df = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')
test_df = t_df.drop(['labels'], axis=1)
test_df

from sklearn.model_selection import train_test_split
train_df = trainx
train_df.reset_index(drop=True, inplace=True)
test_df.reset_index(drop=True, inplace=True)

class CustomDataset(Dataset):
    def __init__(self, df, root_dir, transform=None, iftest=False):
        self.df = df
        self.root_dir = root_dir
        self.transform = transform
        self.iftest = iftest
    def __len__(self):
        return len(self.df)
    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()
        img_name = self.root_dir + self.df.iloc[idx, 0]
        image = cv2.imread(img_name, cv2.IMREAD_COLOR)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        if self.transform:
            image = self.transform(image=image)['image']
        if self.iftest:
            return image
        labels = torch.tensor(np.argmax(self.df.iloc[idx, 1:].values))
        return (image, labels)

IMSIZE = 545
IMSIZE = EfficientNet.get_image_size('efficientnet-b5')

train_dataset = CustomDataset(df=train_df, root_dir='../input/plant-pathology-2021-fgvc8/train_images/', transform=Compose([augmentations.geometric.resize.Resize(height=IMSIZE, width=IMSIZE, always_apply=True), HorizontalFlip(p=0.5), VerticalFlip(p=0.5), ShiftScaleRotate(rotate_limit=25.0, p=0.7), OneOf([Emboss(p=1), Sharpen(p=1), Blur(p=1)], p=0.5), PiecewiseAffine(p=0.5), Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225), always_apply=True), ToTensorV2()]))

test_dataset = CustomDataset(df=test_df, root_dir='../input/plant-pathology-2021-fgvc8/test_images/', transform=Compose([augmentations.geometric.resize.Resize(height=IMSIZE, width=IMSIZE, always_apply=True), Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225), always_apply=True), ToTensorV2()]), iftest=True)

BATCH_SIZE = 1
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)

use_cuda = torch.cuda.is_available()
if use_cuda:
    device = 'cuda:0'
use_tpu = False
use_device = True
if use_tpu:
    device = 'idk'

from efficientnet_pytorch import EfficientNet
model_efficient = EfficientNet.from_pretrained('efficientnet-b7')

ad = False
model_efficient._fc = nn.Sequential(nn.Linear(model_efficient._fc.in_features, 1000, bias=True), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(1000, 6, bias=True))
if use_device:
    model_efficient = model_efficient.to(device)

NEPOCHS = 1
criterion_transfer = nn.CrossEntropyLoss()
learning_rate = 0.0008
optimizer_transfer = optim.AdamW(model_efficient.parameters(), learning_rate, weight_decay=0.001)
num_train_steps = int(len(train_dataset) / BATCH_SIZE * NEPOCHS)
from transformers import get_cosine_schedule_with_warmup
scheduler = get_cosine_schedule_with_warmup(optimizer_transfer, num_warmup_steps=len(train_dataset) / BATCH_SIZE * 5, num_training_steps=num_train_steps)

model_efficient.save('first_weights.h5')
print('Saved model to disk')