Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<define_variables>
!pip install icevision[all]
!pip install matplotlib==3.1.3
!pip install tqdm==4.45.0
network_name = network_name
version_name = version_name
DEVICE = DEVICE
PATH = PATH
SAVE_PATH = SAVE_PATH
MODEL_PATH = MODEL_PATH
IMAGE_FILE_NAME = ['train_images_320x512.npy', 'test_images_320x512.npy']
IMG_SHAPE =(1365, 2048, 3)
 INPUT_IMG_SHAPE =(320, 512, 3)
 IMG_MEAN = np.array([0])
 IMG_STD = np.array([1])
 n_fold = 5
fold =(0,)
 BATCH_SIZE = 16 if not USE_TPU else 64
TEST_BATCH_SIZE = 1
accumulation_steps = 1
loss_weights =(1, 1)
 learning_rate = 1e-3
lr_ratio = np.sqrt(0.1)
 reduce_lr_metric = ['loss', 'score', 'both'][0]
patience = 5
warm_up_steps = 1
warm_up_lr_factor = 0.1
num_classes = 4
n_epochs = 50
train_epochs = 50
resume = False
pretrained = not resume<load_from_csv>
warnings.filterwarnings('ignore' )
df_train = pd.read_csv(PATH + 'train.csv')
 df_train['class'] = np.argmax(df_train.iloc[:, 1:].values, axis=1)
 skf = StratifiedKFold(n_fold, shuffle = True, random_state = 644)
 for i_fold,(train_idx, val_idx)in enumerate(skf.split(df_train, df_train['class'].values)) :
df_train.loc[val_idx, 'fold'] = i_fold
df_train['fold'] = df_train['fold'].astype(int )<save_to_csv>
SEED = 2021
DEBUG = False
IMG_DIM = 512
BATCH_SIZE = 16
NUM_WORKERS = 4
N_FOLDS = 5
FOLDS_IDS = [0]
LR = 1e-4
NUM_EPOCHS = 20
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed_everything(SEED )
class PlantPathologyDataset(Dataset):
def __init__(self, csv, idx, mode, transform = None, data = None):
self.csv = csv.reset_index(drop = True)
 self.data = data
self.filepath_format = PATH + 'images/{}.jpg'
self.idx = np.asarray(idx ).astype('int')
 self.mode = mode
self.transform = transform
def __len__(self):
return self.idx.shape[0]
def __getitem__(self, index):
index = self.idx[index]
if self.data is not None:
image = self.data[index]
else:
img_name = self.csv['image_id'].iloc[index]
image = cv2.imread(self.filepath_format.format(img_name))
if image.shape != IMG_SHAPE:
image = cv2.rotate(image, rotateCode = cv2.ROTATE_90_CLOCKWISE)
 if image.shape != INPUT_IMG_SHAPE:
image = cv2.resize(image, INPUT_IMG_SHAPE[1::-1], interpolation = cv2.INTER_AREA)
 image = image.astype('uint8')
 image_origin = image.copy().astype('float32')
 image = self.transform(image ).astype('float32')if self.transform is not None else image.astype('float32')
 image, image_origin = np.rollaxis(image, 2, 0)/ 255, np.rollaxis(image_origin, 2, 0)/ 255
if self.mode == 'test':
return torch.tensor(image)
 else:
label = self.csv.iloc[index, 1:5].values.astype('float32')
 return torch.tensor(image), torch.tensor(image_origin), torch.tensor(label)
 def get_train_val_dataloader(i_fold, transforms_train, transforms_val):
train_idx, valid_idx = np.where(( df_train['fold'] != i_fold)) [0], np.where(( df_train['fold'] == i_fold)) [0]
train_data = np.load(PATH + IMAGE_FILE_NAME[0])if os.path.isfile(PATH + IMAGE_FILE_NAME[0])else None
dataset_train = PlantPathologyDataset(df_train, train_idx, 'train', transform=transforms_train, data = train_data)
 dataset_valid = PlantPathologyDataset(df_train, valid_idx, 'val', transform=transforms_val, data = train_data)
 batch_size, train_sampler, drop_last = BATCH_SIZE, RandomSampler(dataset_train), False
if USE_TPU and xm.xrt_world_size() > 1:
batch_size, drop_last = BATCH_SIZE // xm.xrt_world_size() , True
train_sampler = torch.utils.data.distributed.DistributedSampler(
 dataset_train, num_replicas=xm.xrt_world_size() , rank=xm.get_ordinal() , shuffle=True 
)
 train_loader = DataLoader(dataset_train, batch_size, sampler = train_sampler, drop_last = drop_last, num_workers=4)
 val_loader = DataLoader(dataset_valid, TEST_BATCH_SIZE, num_workers=4)
 return train_loader, val_loader
def get_test_dataloader() :
test_data = np.load(PATH + IMAGE_FILE_NAME[1])if os.path.isfile(PATH + IMAGE_FILE_NAME[1])else None
dataset_test = PlantPathologyDataset(df_test, np.arange(len(df_test)) , 'test', data = test_data)
 test_loader = DataLoader(dataset_test, TEST_BATCH_SIZE, sampler=None, num_workers=4)
 return test_loader<prepare_x_and_y>
source = Path(".. /input/vinbigdata-512-png")
 train_df = pd.read_csv(source / 'train.csv')
 test_df = pd.read_csv(source / 'test.csv')
 submission_df = pd.read_csv(source / 'sample_submission.csv')
 submission_df.head()
def rand_bbox(img_shape, lam):
H, W = img_shape
cut_rat = np.sqrt(1.- lam)
 cut_w, cut_h = int(W * cut_rat), int(H * cut_rat)
 cx, cy = np.random.randint(cut_w // 4, W - cut_w // 4), np.random.randint(cut_h // 4, H - cut_h // 4)
 bbx1 = np.clip(cx - cut_w // 2, 0, W)
 bby1 = np.clip(cy - cut_h // 2, 0, H)
 bbx2 = np.clip(cx + cut_w // 2, 0, W)
 bby2 = np.clip(cy + cut_h // 2, 0, H)
 return bbx1, bby1, bbx2, bby2
class MixupBatch(object):
def __init__(self, mixup_prob, mixup_alpha, **kwargs):
self.prob = mixup_prob
self.alpha = mixup_alpha
def set_prob(self, epoch, max_epoch):
self.prob = min(1., epoch / max_epoch)
 def __call__(self, ori_img_batch, img_batch, label_batch):

 label_batch_mix, lam = None, 1
if self.alpha <= 0 or np.random.rand() > self.prob:
return img_batch, label_batch, label_batch_mix, lam
batch_size = ori_img_batch.shape[0]
lam = np.random.beta(self.alpha, self.alpha, batch_size)
 lam = np.maximum(1 - lam, lam)
 lam = torch.from_numpy(lam ).view(batch_size, 1, 1, 1 ).to(ori_img_batch)
 shuffled_idx = torch.randperm(batch_size)
 ori_img_batch = lam * ori_img_batch +(1 - lam)* ori_img_batch[shuffled_idx]
label_batch_mix = [label[shuffled_idx] for label in label_batch]
return ori_img_batch, label_batch, label_batch_mix, lam
class CutmixBatch(object):
def __init__(self, cutmix_prob, cutmix_alpha, **kwargs):
self.prob = cutmix_prob
self.alpha = cutmix_alpha
def set_prob(self, epoch, max_epoch):
self.prob = min(1., epoch / max_epoch)
 def __call__(self, ori_img_batch, img_batch, label_batch):

 label_batch_mix, lam = None, 1
if self.alpha <= 0 or np.random.rand() > self.prob:
return img_batch, label_batch, label_batch_mix, lam
batch_size = ori_img_batch.shape[0]
lam = np.random.beta(self.alpha, self.alpha, batch_size)
 lam = np.maximum(1 - lam, lam)
 lam = torch.from_numpy(lam ).view(batch_size, 1, 1, 1 ).to(ori_img_batch)
 shuffled_idx = torch.randperm(batch_size)
 y = ori_img_batch[shuffled_idx].clone().detach()
label_batch_mix = [label[shuffled_idx] for label in label_batch]
for i in range(batch_size):
bbx1, bby1, bbx2, bby2 = rand_bbox(ori_img_batch.shape[-2:], lam[i])
 ori_img_batch[i, :, bby1:bby2, bbx1:bbx2] = y[i, :, bby1:bby2, bbx1:bbx2]
lam[i] = 1 -(bbx2 - bbx1)*(bby2 - bby1)/ np.prod(ori_img_batch.shape[-2:])
 return ori_img_batch, label_batch, label_batch_mix, lam
class FMixBatch(object):
def __init__(self, fmix_prob, fmix_alpha=1, decay_power=3, max_soft=0.0, reformulate=False):
self.prob = fmix_prob
self.alpha = fmix_alpha
self.decay_power = decay_power
self.max_soft = max_soft
self.reformulate = reformulate
def set_prob(self, epoch, max_epoch):
self.prob = min(1., epoch / max_epoch)
 def __call__(self, ori_img_batch, img_batch, label_batch):

 label_batch_mix, lam = None, 1
if np.random.rand() > self.prob:
return img_batch, label_batch, label_batch_mix, lam
size = ori_img_batch.shape[-2:]
lam, mask = sample_mask(self.alpha, self.decay_power, size, self.max_soft, self.reformulate)
 mask = torch.from_numpy(mask ).to(ori_img_batch)
 shuffled_idx = torch.randperm(ori_img_batch.size(0))
ori_img_batch = mask * ori_img_batch +(1 - mask)* ori_img_batch[shuffled_idx]
label_batch_mix = [label[shuffled_idx] for label in label_batch]
return ori_img_batch, label_batch, label_batch_mix, lam
class MixBatch(object):
def __init__(self, transforms_dict = None, img_mean = np.zeros(1), img_std = np.ones(1), mixup_alpha = 0.4, cutmix_alpha = 1.0, **kwargs):
transforms_dict = transforms_dict or {}
self.mix_prob = transforms_dict.get('mix_prob', 0)
 probs = [transforms_dict.get('mixup_prob', 0), transforms_dict.get('cutmix_prob', 0),
transforms_dict.get('fmix_prob', 0), transforms_dict.get('grid_prob', 0)]
self.probs = np.cumsum(probs)
 cutout_ratio = transforms_dict.get('cutout_ratio',(0.05, 0.25))
self.mixup = MixupBatch(mixup_prob = 1, mixup_alpha = mixup_alpha)
 self.cutmix = CutmixBatch(cutmix_prob = 1, cutmix_alpha = cutmix_alpha)
 self.fmix = FMixBatch(fmix_prob = 1, fmix_alpha=1, decay_power=3, max_soft=0.0, reformulate=False)
 self.gridmask = [GridMaskBatch(num_grid =(3, 6), rotate = 15, mode = 0, prob = 1.) ,
GridMaskBatch(num_grid =(3, 6), rotate = 15, mode = 1, prob = 1.) ,
GridMaskBatch(num_grid =(3, 6), rotate = 15, mode = 2, prob = 1.) ,]
def set_prob(self, epoch, max_epoch = 15):
self.mixup.set_prob(epoch, max_epoch)
 self.cutmix.set_prob(epoch, max_epoch)
 self.fmix.set_prob(epoch, max_epoch)
 for gridmask in self.gridmask:
gridmask.set_prob(epoch, max_epoch)
 def __call__(self, ori_img_batch, img_batch, label_batch):
label_batch_mix, lam = None, 1
if np.random.rand() < self.mix_prob:
r = np.random.rand()
if r < self.probs[0]:
return self.mixup(ori_img_batch, img_batch, label_batch)
 elif r < self.probs[1]:
return self.cutmix(ori_img_batch, img_batch, label_batch)
 elif r < self.probs[2]:
return self.fmix(ori_img_batch, img_batch, label_batch)
 elif r < self.probs[3]:
img_batch = np.random.choice(self.gridmask )(ori_img_batch, img_batch)
 return img_batch, label_batch, label_batch_mix, lam<set_options>
class VinBigDataFilepathParser(parsers.Parser, parsers.FilepathMixin):
pass
VinBigDataFilepathParser.generate_template()
transforms_train, transforms_val = None, None
if TRAIN_MODE:
!pip install --upgrade -q albumentations
!pip install --upgrade -q imgaug
transforms_train = transforms.Compose([
lambda image: iaa.Sequential([
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
] )(image=image),
lambda image: albumentations.OneOf([
albumentations.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=20, p=1),
albumentations.RandomResizedCrop(INPUT_IMG_SHAPE[0], INPUT_IMG_SHAPE[1], scale=(0.9, 1.1), p=1),
] )(image=image)['image'],
lambda image: iaa.Sequential([
iaa.Sometimes(0.7, iaa.Cutout(nb_iterations=1, size=(0.6, 0.7), fill_mode="constant", cval=0))
] )(image=image),
])
 mix = None<categorify>
class VinBigDataFilepathParser(parsers.Parser, parsers.FilepathMixin):
def __init__(self, df, source):
self.df = df
self.source = source
def __iter__(self):
yield from self.df.itertuples()
def __len__(self):
return len(self.df)
 def imageid(self, o)-> Hashable:
return o.image_id
def filepath(self, o)-> Union[str, Path]:
return self.source / f"{o.image_id}.png"
def image_width_height(self, o)-> Tuple[int, int]:
return get_image_size(self.filepath(o))
class RGBNorm(nn.Module):
IMAGENET_MEAN = torch.tensor(( 0.485, 0.456, 0.406)).view(1, 3, 1, 1)
 IMAGENET_STD = torch.tensor(( 0.229, 0.224, 0.225)).view(1, 3, 1, 1)
 def forward(self, x):
_, C, _, _ = x.shape
if C == 1:
x = x.repeat(( 1, 3, 1, 1))
x =(x - self.IMAGENET_MEAN.to(x)) / self.IMAGENET_STD.to(x)
 return x
class Mish(nn.Module):
def __init__(self, *args, **kwagrs):
super(Mish, self ).__init__()
def forward(self,x):
return x.mul(F.softplus(x ).tanh())
 def gem(x, kernel_size, stride = None, p = 3, eps = 1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p), kernel_size, stride ).pow(1./p)
 class ArcMarginProduct(nn.Module):
def __init__(self, in_features, out_features):
super().__init__()
self.weight = nn.Parameter(torch.FloatTensor(out_features, in_features))
self.reset_parameters()
def reset_parameters(self):
stdv = 1./ math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
 def forward(self, features):
cosine = F.linear(F.normalize(features), F.normalize(self.weight))
return cosine
class Classifier(nn.Module):

 def __init__(self, backbone, num_classes, **kwargs):
super().__init__()
self.norm = RGBNorm()
self.backbone = backbone
self.gfc = nn.Sequential(*[nn.AdaptiveAvgPool2d(1),
nn.Flatten() ,
nn.Linear(backbone.out_channel, 2048),
Mish() ])
 self.dropouts = nn.ModuleList([nn.Dropout(0.5)for _ in range(5)])
 self.metric_classify = ArcMarginProduct(2048, num_classes)
 self.cls_head = nn.Linear(2048, num_classes)
 def forward(self, x):
x = self.norm(x)
 x = self.backbone(x)
 x = self.gfc(x)
 for i, dropout in enumerate(self.dropouts):
if i == 0:
out = self.cls_head(dropout(x))
else:
out += self.cls_head(dropout(x))
out /= len(self.dropouts)
 metric_output = self.metric_classify(x)
 return out, metric_output
def load_pretrained_model(model, model_path = '', url = '', skip =() , conversion =()):
if os.path.isfile(model_path):
state_dict = torch.load(model_path, map_location = 'cpu')
 if not USE_TPU or xm.is_master_ordinal() :
print('=> loading pretrained model {}'.format(model_path))
elif url != '':
state_dict = model_zoo.load_url(url, progress = False, map_location = 'cpu')
 if not USE_TPU or xm.is_master_ordinal() :
print('=> loading pretrained model {}'.format(url))
else:
return
conversion = np.array(conversion ).reshape(-1, 2)if len(conversion)else []
model_dict = model.state_dict()
pretrained_state_dict = {}
for ks in state_dict.keys() :
if ks in model_dict.keys() and all(s not in ks for s in skip):
km = ks
for _km, _ks in conversion:
if ks == _ks:
km = _km
break
pretrained_state_dict[km] = state_dict[ks]
if not USE_TPU or xm.is_master_ordinal() :
print(f"=> loading pretrained model weight length {len(pretrained_state_dict)} / total_state_dict {len(state_dict)} / total_model_dict {len(model_dict)}")
 model_dict.update(pretrained_state_dict)
 model.load_state_dict(model_dict, strict = False )<choose_model_class>
parser = VinBigDataFilepathParser(submission_df, source / "test")
 records = parser.parse(data_splitter=None, idmap=None, autofix=True, show_pbar=True, cache_filepath=None)
 test_rs = records[0] + records[1]
def efficientnet_b4(pretrained = False, **kwargs):
if not USE_TPU or xm.is_master_ordinal() :
print('=> create `efficientnet_b4`')
 backbone = EfficientNet.from_name('efficientnet-b4')
 backbone._fc = None
backbone.out_channel = backbone.out_channels
backbone.forward = backbone.forward_features
if pretrained:
pretrained_file_name = ''
url = 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b4-6ed6700e.pth'
load_pretrained_model(backbone, model_path = MODEL_PATH + pretrained_file_name, url = url,
skip =('.num_batches_tracked', '_fc.',))
model = Classifier(backbone, num_classes)
 return model<compute_train_metric>
presize = 512
size = 384
infer_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(size), tfms.A.Normalize() ] )
def cross_entropy(preds, trues, reduction = 'mean', **kwargs):
if reduction == 'mean':
return -torch.sum(trues * F.log_softmax(preds, dim = 1), dim = 1 ).mean()
elif reduction == 'sum':
return -torch.sum(trues * F.log_softmax(preds, dim = 1), dim = 1 ).sum()
elif reduction == 'none':
return -torch.sum(trues * F.log_softmax(preds, dim = 1), dim = 1)
 def focol_loss(preds, trues, alpha = 4, gamma = 2, reduction = 'mean', **kwargs):
probs = F.softmax(preds, dim = 1)
 pos_loss = -torch.sum(trues * alpha *(1 - probs)** gamma * torch.log(probs), dim = 1)
 if reduction == 'mean':
return pos_loss.mean()
elif reduction == 'sum':
return pos_loss.sum()
elif reduction == 'none':
return pos_loss
def ohem_loss(preds, trues, ohem_rate = 1, reduction = 'mean', **kwargs):
ohem_rate = max(0, min(1, ohem_rate))
loss = -torch.sum(trues * F.log_softmax(preds, dim = 1), dim = 1)
 loss = torch.topk(loss, int(ohem_rate * loss.size(0)))[0] if 0 <= ohem_rate < 1 else loss
if reduction == 'mean':
return torch.mean(loss)
 elif reduction == 'sum':
return torch.sum(loss)
 elif reduction == 'none':
return loss
class ArcFaceLoss(nn.Module):
def __init__(self, s=30.0, m=0.5, reduction='mean'):
super().__init__()
self.reduction = reduction
self.s = s
self.cos_m = math.cos(m)
 self.sin_m = math.sin(m)
 self.th = math.cos(math.pi - m)
 self.mm = math.sin(math.pi - m)* m
def forward(self, logits, labels):
logits = logits.float()
cosine = logits
sine = torch.sqrt(1.0 - torch.pow(cosine, 2))
phi = cosine * self.cos_m - sine * self.sin_m
phi = torch.where(cosine > self.th, phi, cosine - self.mm)
 output =(labels * phi)+(( 1.0 - labels)* cosine)
 output *= self.s
loss = cross_entropy(output, labels, self.reduction)
 return loss / 2
def criterion(logits, metric_logits, trues, is_val = False, lam = 1, **kwargs):
assert len(trues)== len(logits)== len(metric_logits)
 weights = loss_weights
if not is_val:
loss_0 = cross_entropy(logits, trues.float() , reduction='mean')* lam
loss_metric = ArcFaceLoss(reduction='mean' )(metric_logits, trues.float())* lam
loss =(loss_0 * weights[0] + loss_metric * weights[1])/ sum(weights)
 else:
loss_0 = cross_entropy(logits, trues.float() , reduction='sum')* lam
loss_metric = ArcFaceLoss(reduction='sum' )(metric_logits, trues.float())* lam
loss =(loss_0 * weights[0] + loss_metric * weights[1])/ sum(weights)
 return loss,(loss_0.detach() , loss_metric.detach() )<compute_test_metric>
infer_ds = Dataset(test_rs, infer_tfms )
def get_score(submission, solution):
roc_score = roc_auc_score(solution, submission)
 predictions = np.argmax(submission, axis = 1)
 trues = np.argmax(solution, axis = 1)
 hard_idx = np.where(np.max(submission, axis = 1)<= 0.9)[0]
easy_idx = np.where(np.max(submission, axis = 1)> 0.9)[0]
score = np.mean(predictions == trues)
 hard_score = np.mean(predictions[hard_idx] == trues[hard_idx])if len(hard_idx)else 0
easy_score = np.mean(predictions[easy_idx] == trues[easy_idx])if len(easy_idx)else 0
return roc_score, score, hard_score, easy_score
def show_running_time(text):
def decorator(func):
@functools.wraps(func)
 def wrapper(*args, **kw):
t0 = time.time()
res = func(*args, **kw)
 print(f"{text} time: {time.time() -t0:.1f} s")
 return res
return wrapper
return decorator
def clear_cache(func):
@functools.wraps(func)
 def wrapper(*args, **kw):
torch.cuda.empty_cache()
gc.collect()
return func(*args, **kw)
 return wrapper
@show_running_time('train')
 @clear_cache
def train_epoch(model, loader, device, optimizer, verbose = False):
model.train()
train_loss = []
optimizer.zero_grad()
acc_steps = accumulation_steps
steps = len(loader)// acc_steps * acc_steps
progress_bar = tqdmauto(loader)if(not USE_TPU or xm.is_master_ordinal())and verbose else None
for batch_idx,(img_batch, origin_img_batch, label_batch)in enumerate(loader):
label_batch_mix, lam = None, 1
if mix is not None:
img_batch, label_batch, label_batch_mix, lam = mix(img_batch, origin_img_batch, label_batch)
 img_batch = img_batch.to(device)
 label_batch = label_batch.to(device)
 label_batch_mix = None if label_batch_mix is None else label_batch_mix.to(device)
 lam = lam.to(device)if isinstance(lam, torch.Tensor)else lam
if steps is not None and batch_idx >= steps:
acc_steps = 1
logits, metric_logits = model(img_batch)
 loss, _ = criterion(logits, metric_logits, label_batch, is_val = False, lam = lam)
 if label_batch_mix is not None:
loss_mix = criterion(logits, metric_logits, label_batch_mix, is_val = False, lam = 1 - lam)
 loss = loss + loss_mix
loss = loss / acc_steps
loss.backward()
if(batch_idx + 1)% acc_steps == 0:
if USE_TPU:
xm.optimizer_step(optimizer, barrier = True)
 else:
optimizer.step()
optimizer.zero_grad()
loss_np = loss.detach().cpu().item() * acc_steps
train_loss.append(loss_np)
 if(not USE_TPU or xm.is_master_ordinal())and verbose and(batch_idx <= 10 or(batch_idx - 10)% verbose_step == 0):
progress_bar.set_postfix_str(f"loss: {loss_np:.4f}, smooth_loss: {np.mean(train_loss[-20:]):.4f}")
 progress_bar.update(1 if batch_idx <= 10 else 30)
 return np.asarray(train_loss ).mean()
@show_running_time('val')
 @clear_cache
def val_epoch(model, loader, device):
model.eval()
val_loss, val_loss1, val_loss2 = 0, 0, 0
preds, metric_preds, trues = [], [], []
with torch.no_grad() :
for img_batch, origin_img_batch, label_batch in loader:
img_batch, label_batch = img_batch.to(device), label_batch.to(device)
 logits, metric_logits = model(img_batch)
 loss,(loss1, loss2)= criterion(logits, metric_logits, label_batch, is_val = True)
 val_loss += loss.detach().cpu().item()
val_loss1 += loss1.detach().cpu().item()
val_loss2 += loss2.detach().cpu().item()
preds.append(F.softmax(logits, dim = 1 ).cpu().numpy())
 metric_preds.append(F.softmax(metric_logits, dim = 1 ).cpu().numpy())
 trues.append(label_batch.cpu().numpy())
 preds = np.concatenate(preds)
 metric_preds = np.concatenate(metric_preds)
 trues = np.concatenate(trues)
 scores1 = get_score(preds, trues)
 scores2 = get_score(metric_preds, trues)
 val_result =(val_loss / len(trues), val_loss1 / len(trues), val_loss2 / len(trues), preds, metric_preds, trues, scores1, scores2)
 return val_result<load_pretrained>
backbone = backbones.resnet_fpn.resnext50_32x4d(pretrained=False)
 model = faster_rcnn.model(backbone=backbone, num_classes=len(class_map))
checkpoint_path = '.. /input/vinbigdata-eda-faster-rcnn-icevision-training/frcnn-best-model-fold0-epoch=58.ckpt'
checkpoint = torch.load(checkpoint_path, map_location=torch.device(DEVICE))
new_state_dict = OrderedDict()
for key, value in checkpoint['state_dict'].items() :
if key.startswith('model.'):
new_key = key[6:]
new_state_dict[new_key] = value
else:
new_state_dict[key] = value
model.load_state_dict(new_state_dict, strict=False )
def resume_model(model, optimizer = None, lr_scheduler = None, resume_file_path = '', device = 'cpu'):
other_param = {}
history, last_epoch, best_val_loss, best_val_score = None, None, None, None
if os.path.isfile(resume_file_path):
checkpoint = torch.load(resume_file_path, map_location = device)
 model.load_state_dict(checkpoint.pop('state_dict'), strict = False)
 print(f"load model success! last_epoch: {checkpoint['last_epoch']}")
 if optimizer is not None and lr_scheduler is not None:
optimizer.load_state_dict(checkpoint['optimizer'])
 lr_scheduler.load_state_dict(checkpoint['lr_scheduler'])
 print(f"lr: {optimizer.state_dict() ['param_groups'][0]['lr']:.2e}")
 checkpoint.pop('optimizer', None)
 checkpoint.pop('lr_scheduler', None)
 other_param.update(checkpoint)
 print(f"best_val_score: {other_param['best_val_score']:.4f}, " +
f"best_val_loss: {other_param['best_val_loss']:.4f}
")
 return model, optimizer, lr_scheduler, other_param<choose_model_class>
infer_dl = faster_rcnn.infer_dl(infer_ds, batch_size=1, shuffle=False)
 _, preds = faster_rcnn.predict_dl(model=model, infer_dl=infer_dl, detection_threshold=0.5 )
model_class = efficientnet_b4<train_model>
for idx, pred in tqdm(enumerate(preds)) :
if len(pred['labels'])== 0:
continue
image_idx = dict(test_rs[idx])['imageid']
image_id = submission_df.iloc[image_idx]['image_id']
original_width, original_height = test_df.loc[test_df['image_id'] == image_id][['width', 'height']].values[0]
s = ''
for label, score, bbox in zip(pred['labels'], pred['scores'], pred['bboxes']):
bbox = vars(bbox)
 xmin = int(bbox['xmin']*(original_width/size))
ymin = int(bbox['ymin']*(original_height/size))
xmax = int(bbox['xmax']*(original_width/size))
ymax = int(bbox['ymax']*(original_height/size))
values_list = [label, np.round(score, 2), xmin, ymin, xmax, ymax]
s += ' '.join(map(str, values_list))
s += ' '
submission_df.iloc[image_idx]['PredictionString'] = s.strip()
<train_model><EOS> 
submission_df.to_csv('submission.csv', index=False )